#1054 - “字段列表”中的未知列'ord'

时间:2015-05-13 16:20:21

标签: php mysql

我正在使用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

2 个答案:

答案 0 :(得分:1)

看起来您正在设置一些订购号,它需要为

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

答案 1 :(得分:0)

它应该是' 0 AS ord'或者' 1 AS ord'。此外,当在UNION ALL上进行排序时,我建议将所有内容放在子查询中并对结果进行排序,因为UNION上的排序会给您带来意想不到的结果。

SELECT
    *
FROM (
    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 * 
            FROM tkts_follows AS t2f 
            WHERE t2f.tktID = tkt.id
                AND t2f.type = 'pin'
                AND t2f.userID = 1
        ) 
) AS UNIONS
ORDER BY ord, last_update DESC
LIMIT 0, 10