这是因为您不能在WHERE
andORDER BY
子句中使用别名
所以你必须做类似的事情
SELECT *, IF(c.status = '1', "green", "red") AS colour
FROM (
SELECT *,'calls1' AS 'tbl', id AS cid FROM calls1
WHERE uid LIKE '%1111%'
ORDER BY timestamp DESC
UNION
SELECT *,'calls2' AS 'tbl', id AS cid FROM calls2
WHERE uid LIKE '%1111%'
ORDER BY timestamp DESC
) as c
LEFT JOIN objects AS o ON o.call_id = c.id
WHERE c.status = 'the color id you want' // or you can rewrite your if here
ORDER BY c.status ASC, c.timestamp DESC
或者这用于您的简化查询
SELECT *, "red" AS colour
FROM (
SELECT *,'calls' AS 'tbl', id AS cid FROM calls) as c
WHERE c.status = 'the color id you want'
ORDER BY c.created_date DESC
如果总是红色,为什么要按颜色排序?