使用2 JOINS仍然在表中显示行是不存在连接表中的行

时间:2017-08-16 08:07:45

标签: mysql

我正在运行此查询,而m2上的第二个联接用于排序结果,但如果admin表中的website_order行,我仍希望显示m2表中的行1}}不存在

SELECT 
    a.sequence, 
    CONCAT(a.forename, ' ', a.surname) AS name, 
    a.position 
FROM 
    admin a 
JOIN 
    admin_meta m1 on a.sequence = m1.user 
JOIN 
    admin_meta m2 ON a.sequence = m2.user 
WHERE 
    a.disabled = '' AND 
    m1.field = 'display_on_website' AND 
    m1.value = '1' AND 
    m2.field = 'website_order' 
ORDER BY CAST(m2.value AS DECIMAL)

1 个答案:

答案 0 :(得分:1)

使用left join

SELECT 
    a.sequence, 
    CONCAT(a.forename, ' ', a.surname) AS name, 
    a.position 
FROM 
    admin a 
JOIN 
    admin_meta m1 on a.sequence = m1.user 
LEFT JOIN 
    admin_meta m2 ON a.sequence = m2.user AND m2.field = 'website_order'
WHERE 
    a.disabled = '' AND 
    m1.field = 'display_on_website' AND 
    m1.value = '1' 

ORDER BY CAST(m2.value AS DECIMAL)