Je souhaite obtenir des résultats via une requête similaire à:
SELECT
* FROM
users LEFT JOIN
IF (users.type = '1', 'private','company') AS details ON
users.id = details.user_id WHERE
users.id = 1
Des idées?
SELECT * FROM users
LEFT JOIN private AS details ON users.id = details.user_id
WHERE users.id = 1 AND users.type = 1
UNION
SELECT * FROM users
LEFT JOIN company AS details ON users.id = details.user_id
WHERE users.id = 1 AND users.type != 1
Je pense que c'est ce que vous essayez de faire, n'est-ce pas?
Comme vous venez de dire que le nombre de colonnes diffère, vous devez spécifier les colonnes, par exemple.
SELECT 'private' AS detailType, users.*, col1, col2, col3, '' FROM users
LEFT JOIN private AS details ON users.id = details.user_id
WHERE users.id = 1 AND users.type = 1
UNION
SELECT 'company', users.*, col1, '', '', col4 FROM users
LEFT JOIN company AS details ON users.id = details.user_id
WHERE users.id = 1 AND users.type != 1
Dans cet exemple, private a les colonnes col1, col2 et col3, alors que company a les colonnes col1 et col4, mais vous les voulez toutes.
Je suis sûr que cela est déjà résolu, mais pour les personnes ayant un problème similaire.
Vous pouvez également essayer plusieurs jointures à gauche pour obtenir toutes les données
SELECT *, IF (users.type = 1, p.name, c.name) AS name FROM users
LEFT JOIN private AS p ON (users.type = 1 AND users.id = p.user_id)
LEFT JOIN company AS c ON (users.type != 1 AND users.id = c.user_id)
SELECT
users.*,
details.info,
CASE users.type WHEN '1' THEN 'private' ELSE 'company' END AS user_type
FROM
users
INNER JOIN (
SELECT user_id, info FROM private
UNION
SELECT user_id, info FROM company
) AS details ON details.user_id = users.id
EDIT: Version originale de la réponse (question mal comprise):
SELECT
*,
CASE type WHEN '1' THEN 'private' ELSE 'company' END AS details
FROM
users
WHERE
users.id = 1
Ici, je partage la condition if else dans la requête SQL de Magento2 pour obtenir les détails de la commande de rapport que les clients se connectent et ne se connectent pas aux deux, vous venez d'améliorer votre requête en fonction de la situation.
SELECT so.increment_id as ID,sog.customer_id as Customer_Id, sog.customer_name as Customer_Name, sog.customer_email as Customer_Email,CASE sog.customer_group WHEN '1' THEN 'Custome Login' ELSE 'Not Login' END as Customer_Group, sog.grand_total as Grand_Total,sog.subtotal as Subtotal, sog.billing_name as Billing_Name,sog.billing_address as Billing_Address,sog.shipping_address as shipping_address,so.shipping_description as Shipping_Information, so.status as Status,so.cancel_order_username as Canceled_BY,so.cancel_order_currenttime as Cancellation_Time, so.cancel_order_comment as Cancellation_Reason from sales_order so LEFT JOIN sales_order_grid as sog ON sog.increment_id=so.increment_id where so.cancel_order_currenttime >= Date('2018-10-01') AND so.cancel_order_currenttime <= Date('2018-12-05')
Here we have created some alias according to the situation:-
so->sales_order table,
sog->sales_order_grid,
and we are using the if/else condition in the customer group because we know that, "0" is used for the Guest user,
"1" is used for the login user and both are used for the customer group:-
J'espère que cette suggestion vous aidera avec votre confusion, et s'il vous plaît laissez-moi savoir si vous rencontrez un problème pour comprendre cet article.