简体   繁体   English

MySQL联合和命令的帮助

[英]MySQL union and order by help

The below code works but when i change Order by id to Order by s.id, i get this error 下面的代码工作,但当我通过id将Order更改为Order by s.id时,我收到此错误

Unknown column 's.id' in 'order clause' 'order order'中的未知列's'id'

$construct =  "SELECT child.* FROM products child LEFT JOIN products parent on parent.name=child.parent INNER JOIN subscribe s ON (s.productid = parent.id) WHERE s.username='$logged' AND s.type='0'
        UNION
        SELECT child.* FROM products child LEFT JOIN products parent on parent.sid=child.sid INNER JOIN subscribe s ON (s.productid = parent.id) WHERE s.username='$logged' AND parent.keyword != child.name AND s.type='1'
        ORDER BY s.id DESC";

How can i change the code so it is ordered by s.id, which is the subscribe table's id? 我如何更改代码,以便s.id排序,这是订阅表的ID?

MySQL is trying to apply the ORDER BY to the UNION but the UNION only has the child columns (without the child. prefix at that), there is no s.id in the UNION. MySQL正在尝试将ORDER BY应用于UNION,但UNION只有child列(没有child.前缀),UNION中没有s.id But you can add one: 但你可以添加一个:

SELECT child.*, s.id as sid ...
UNION
SELECT child.*, s.id as sid ...
ORDER BY sid DESC

You need to give it an alias as the UNION will strip off the table name or alias prefix. 你需要给它一个别名,因为UNION将剥离表名或别名前缀。 If there is an sid column in child then use something else as the alias for s.id . 如果childsid列,则使用其他内容作为s.id的别名。

"INNER JOIN subscribe s" “INNER JOIN订阅”

It looks like maybe you want: 它看起来像你想要的:

INNER JOIN `subscribe` AS `s`

if that doesn't work, please post the output of: 如果这不起作用,请发布以下输出:

DESCRIBE subscribe;

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

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