MySQL JOIN with IF conditions

后端 未结 4 939
遇见更好的自我
遇见更好的自我 2020-12-28 15:40

I want to get some results via query simillar to:

SELECT 
    * FROM
    users LEFT JOIN
    IF (users.type = \'1\', \'private\',\'company\') AS details ON
          


        
相关标签:
4条回答
  • 2020-12-28 15:58
    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
    

    I think that is what you are trying to do, isn't it?

    As you have now said that the number of columns differs, you would need to specify the columns, e.g.

    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
    

    In this example, private has columns col1, col2 and col3, whilst company has col1 and col4, but you want them all.

    0 讨论(0)
  • 2020-12-28 15:59

    I'm sure this is already resolved, but for people with a similar problem.

    You can also try to multiple left joins to get all the data

    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)
    
    0 讨论(0)
  • 2020-12-28 16:09
    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: Original version of the answer (question misunderstood):

    SELECT
      *, 
      CASE type WHEN '1' THEN 'private' ELSE 'company' END AS details
    FROM
      users
    WHERE
      users.id = 1
    
    0 讨论(0)
  • 2020-12-28 16:14

    Here I am sharing the if else condition in Magento2 SQL query to get the report order details customers log in and not login both, you have just enhanced your query according to the 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.

    I hope this suggestion will help you with your confusion, and please let me know if you face any issue in understanding this answer.

    0 讨论(0)
提交回复
热议问题