我正在运行此查询,而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)
答案 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)