Mysql
 sql >> Database >  >> RDS >> Mysql

MySQL combina due query

Che ne dici

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

Quando usi le sottoquery in un FROM clausola, è come se stessi creando tabelle temporanee. Quindi con l'alias puoi fare riferimento ad esse ed eseguire un INNER JOIN