我在用mySQL。
当我试图将参数添加到SQL查询时出错,该查询应该对我的结果进行排序:
1054-“字段列表”中的未知列“ord”
SELECT tkt.* , ord = 0
FROM tkts_follows AS t2f
JOIN tkts_topics AS tkt ON (t2f.tktID = tkt.id
AND t2f.type = 'pin'
AND t2f.userID = 1 )
UNION ALL
SELECT * , ord = 1
FROM tkts_topics AS tkt
WHERE (1=1 ) AND NOT EXISTS (SELECT *
FROM tkts_follows AS t2f
WHERE t2f.tktID = tkt.id AND t2f.type = 'pin' AND t2f.userID = 1)
ORDER BY ord, last_update DESC LIMIT 0, 10
最佳答案
看起来你在设置订购编号,它必须是
SELECT tkt.* , 0 as ord
FROM tkts_follows AS t2f
JOIN tkts_topics AS tkt ON t2f.tktID = tkt.id
AND t2f.type = 'pin'
AND t2f.userID = 1
UNION ALL
SELECT * , 1 as ord
FROM tkts_topics AS tkt
WHERE 1=1 AND NOT EXISTS (
SELECT 1
FROM tkts_follows AS t2f
WHERE t2f.tktID = tkt.id AND t2f.type = 'pin' AND t2f.userID = 1
)
ORDER BY ord, last_update DESC LIMIT 0, 10