MySQL combine two queries

后端 未结 1 1947
粉色の甜心
粉色の甜心 2021-02-19 21:35

I have two MySQL queries

QUERY:

SELECT sodnik_1 FROM prihodnji_krog  WHERE file_id=\'8778\' AND sodnik_1 != \'\'
UNION 
SELECT sodnik_2 FROM prihodnji_krog  WHE         


        
相关标签:
1条回答
  • 2021-02-19 22:24

    How about

    SELECT * FROM
    (
        SELECT sodnik_1 as value FROM prihodnji_krog  WHERE file_id='8778' AND sodnik_1 != ''
        UNION 
        SELECT sodnik_2 as value FROM prihodnji_krog  WHERE file_id='8778' AND sodnik_2 != ''
        UNION
        SELECT sodnik_3 as value FROM prihodnji_krog  WHERE file_id='8778' AND sodnik_3 != ''
        UNION
        SELECT sodnik_4 as value FROM prihodnji_krog  WHERE file_id='8778' AND sodnik_4 != ''
    ) x INNER JOIN 
    (
        SELECT value FROM notification_sodniki WHERE user_id='16'
    ) y 
    ON x.value = y.value
    

    When you use subqueries in a FROM clause, it's like if you are making temporary tables. Then with the alias you can refer to them and do a INNER JOIN

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