我正在将此查询写入shell脚本,因此所有双引号都被转义,时间戳是变量。
如果我不在终端上使用“order by”,则此查询有效。
还有一件奇怪的事情是,如果执行PHPMYADMIN
中的查询部分,那么"order by
“就可以了。
但如果我在终端中运行此查询,它会给我UNKNOWN COLUMN QID error
SELECT 'GUIDE-NAME','QUESTION-ID','ACTION','TIMESTAMPS'
UNION
SELECT
SUBSTRING(B.msg,LOCATE('q_id',B.msg)+5, (LOCATE('\"',B.msg)+2) ) AS qid, B.ts
FROM TABLE B, TABLE A
WHERE
LOCATE('q_id',B.msg) > 0
AND A.g_id=B.g_id and B.ts > from_unixtime($DateWeekBefore)
AND B.action
IN(\"review_question\",\"send_back",\"send_review\",\"publish\",\"quiet_publish\")
ORDER BY qid
INTO OUTFILE '/tmp/$vr9$DateWeek.csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
答案 0 :(得分:1)
MySQL尝试在所有查询上应用order by
,而不是在子选择上应用(select ....) union (select.... order by something)
。为了分隔子选择使用括号:
INTO OUTFILE
似乎SELECT * FROM (
(
SELECT 'GUIDE-NAME','QUESTION-ID','ACTION','TIMESTAMPS'
) UNION (
SELECT.... ORDER BY qid
)
) a
INTO OUTFILE ....
不能直接在联合中使用,因此我们需要将联合作为子查询:
{{1}}
答案 1 :(得分:0)
qid是别名。而不是
ORDER BY qid
试试这个:
ORDER BY SUBSTRING(B.msg,LOCATE('q_id',B.msg)+5, (LOCATE('\"',B.msg)+2) )
编辑:我的猜测是,PHPADMIN非常聪明,可以看到它是别名并进行翻译,而终端却没有。
答案 2 :(得分:0)
正如Karolis所说,ORDER BY仅适用于您的UNION中的第二个查询。
试试这个:
SELECT *
FROM
(
[your whole query]
) AS some_subquery
ORDER BY qid
INTO OUTFILE ...