How do I use an IF statement in an MySQL join query?

前端 未结 6 1374
情话喂你
情话喂你 2021-02-04 06:09

I have a SQL query that left joins a table in different ways depending on a condition.

SELECT m.id, u.first_name AS otherUser
FROM matches AS m
IF (u.id=m.user2I         


        
相关标签:
6条回答
  • 2021-02-04 06:37

    Use this query:

    SELECT m.id, u.first_name AS otherUser FROM matches AS m LEFT JOIN users AS u ON u.id = m.user1ID AND u.id=m.user2ID LEFT JOIN users AS u1 ON u1.id = m.user2ID WHERE m.user1ID=2 OR m.user2ID=2

    Updated query:

    SELECT m.id, u.first_name AS otherUser
    FROM matches AS m
    LEFT JOIN users AS u ON u.id = (CASE WHEN u.id=m.user2ID 
                                       THEN m.user1ID
                                       ELSE m.user2ID
                                   END)
    WHERE m.user1ID=2 OR m.user2ID=2
    

    Check this Source.

    Also check this MYSQL Inner Join if statement . It might be helpful for you.

    0 讨论(0)
  • 2021-02-04 06:41

    Specify the condition as part of the ON clause:

    SELECT m.id, u.first_name AS otherUser
    FROM matches AS m
    LEFT JOIN users AS u ON (u.id=m.user2ID and u.id = m.user1ID) or (u.id<>m.user2ID and u.id = m.user2ID) 
    WHERE m.user1ID=2 OR m.user2ID=2
    

    Another way to do the same thing:

    SELECT m.id, u.first_name AS otherUser
    FROM matches AS m
    LEFT JOIN users AS u ON IF(u.id=m.user2ID,u.id = m.user1ID,u.id = m.user2ID) 
    WHERE m.user1ID=2 OR m.user2ID=2
    
    0 讨论(0)
  • 2021-02-04 06:41

    The image will help you to find the structure of db..$userId is Uses id of user we wish to see the friend list for.

    SELECT `connections`.*,`usr`.*,"'.$existImage.'" as main_image_url,"'.$existThumb.'" as thumb_image_url FROM `connections` `connections` JOIN `users` `usr` ON `usr`.`userId` = (CASE WHEN `connections`.`requestBy`="'.$userId.'" THEN connections.requestFor ELSE connections.requestBy END) WHERE `connections`.`requestBy` = "'.$userId.'" OR `connections`.`requestFor` = "'.$userId.'" AND `connections`.`requestStatus` = 1
    
    0 讨论(0)
  • 2021-02-04 06:47

    Thanks, It worked for me. Tried something else like below :

    **

    Create table TEMP1 (ID INT,T_MONTH INT,T_YEAR INT)
    INSERT INTO TEMP1 VALUES(1,1,2001)
    INSERT INTO TEMP1 VALUES(2,2,2001)
    INSERT INTO TEMP1 VALUES(3,3,2001)
    

    **

    CREATE TABLE TEMP2 (T_MONTH INT,T_YEAR INT,FREQUENCY CHAR(1),VAL FLOAT)
    INSERT INTO TEMP2 VALUES(1,2001,'M',1.1)
    INSERT INTO TEMP2 VALUES(3,2001,'M',1.2)
    INSERT INTO TEMP2 VALUES(3,2001,'Q',1.3)
    INSERT INTO TEMP2 VALUES(12,2001,'A',1.4)
    
    SELECT * FROM TEMP1 L JOIN TEMP2 H
    ON L.T_YEAR = H.T_YEAR
    OR (L.T_MONTH = (CASE WHEN H.FREQUENCY='M'
                           THEN H.T_MONTH
                      END)
        OR dbo.GetQuarterFromMonth(L.T_MONTH) = (CASE WHEN H.FREQUENCY = 'Q'
                            THEN dbo.GetQuarterFromMonth(H.T_MONTH)
                            END))
    WHERE H.FREQUENCY = 'Q'
    

    Here GetQuarterFromMonth is a user defined funtion which returns Quarter for a particular month.

    The objective of above query is to inflate the value of Quarter for all the months in table 1. If frequency is annual then in that case the value should be inflated for all the months 1-12 of table 1 which is achieved through first join condition. In case frequency is Monthly then each month from table1 should be mapped to table2. The only unique case is handling quarter frequency.

    0 讨论(0)
  • 2021-02-04 06:51

    You can not use the IF THEN ELSE END IF-stuff in a SELECT in this way. However, you can use it in stored procedures and functions.

    I would JOIN u.id with both m.user1ID and m.user2ID and use DISTINCT to avoid duplicates.

    There is a IF() which you can use in SELECTs, but you can not do flow control with IF().

    0 讨论(0)
  • You can also have two LEFT JOIN on the same table like this...

    LEFT JOIN users AS u ON u.id = m.user1ID
    LEFT JOIN users AS u2 ON u2.id = m.user2ID
    

    This is an alternative to using IF statements.

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