MySQL JOIN with IF conditions

后端 未结 4 940
遇见更好的自我
遇见更好的自我 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 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.

提交回复
热议问题