简体   繁体   English

MySQL undefined别名,带有联合选择和别名

[英]MySQL undefined alias with union select and alias

So I have this table, if I use this query everything is fine: 所以我有这个表,如果我使用此查询,一切都很好:

SELECT *
FROM `users` AS usr
WHERE usr.id = 9
ORDER BY usr.id ASC , usr.date DESC
LIMIT 0 , 30

However, as soon as I try to use union select I get an error: 但是,当我尝试使用并集选择时,我得到一个错误:

SELECT *
FROM `users` AS usr   
WHERE usr.id = 9
UNION ALL SELECT 1,2,3,4,5
ORDER BY usr.id ASC , usr.date DESC
LIMIT 0 , 30

I get the error unknown column 'usr.id' in order clause. 我在order子句中收到错误未知列'usr.id'。 How can I fix this so that the union select works with it? 我该如何解决这个问题,以便联合选择可以使用它? Thanks. 谢谢。

EDIT: IS there any way to do it while keeping my original query in tact and not changing it too much? 编辑:有什么办法做到这一点,同时保持我原来的查询完好无损,而又没有改变太多?

Query: 查询:

SELECT a.*
FROM(SELECT *
     FROM `users` AS usr   
     WHERE usr.id = 9
     UNION ALL 
     SELECT 1,2,3,4,5) a
ORDER BY a.id ASC , a.date DESC
LIMIT 0 , 30

Second table doesn't have a column id . 第二个表没有列id Do it like so: 这样做:

SELECT id, ...
FROM `users` AS usr   
WHERE usr.id = 9
UNION ALL SELECT 1 id, ...
ORDER BY id ASC... DESC
LIMIT 0 , 30

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM