通过不在MySQL 5.1中工作来订购

通过不在MySQL 5.1中工作来订购,mysql,sql-order-by,where,sql-like,Mysql,Sql Order By,Where,Sql Like,我对php中的sql查询有一个问题: select user, name, outlet, switch, port, vlan, mac, status from access where user like '%' and name like '%' and outlet like '%' and switch like '%' and port like '%' and vlan like '%' and mac like

我对php中的sql查询有一个问题:

select
    user, name, outlet, switch, port, vlan, mac, status
from access where 
    user like '%'
    and name like '%'
    and outlet like '%'
    and switch like '%'
    and port like '%'
    and vlan like '%'
    and mac like '%'
    and status like '%'
order by 'user';
在MySQL客户端版本5.1.36上运行查询时,查询无法完全工作(ORDER BY无法工作),但在MySQL客户端版本4.1.13上运行相同查询时,ORDER BY可以工作


我已经检查了几乎所有关于orderby、WHERE和commands的手册,但没有结果。不要提及版本差异等。

我认为您需要的是:

SELECT `user`,`name`,`outlet`,`switch`,`port`,`vlan`,`mac`,`status` 
FROM `access`
WHERE `user` like '%' 
    AND `name` like '%'
    AND `outlet` like '%'
    AND `switch` like '%'
    AND `port` like '%'
    AND `vlan` like '%'
    AND `mac` like '%'
    AND `status` like '%' 
ORDER BY `user`;
虽然我不明白你的
WHERE
条款。它不会过滤任何字段。
编辑您的一些列名(
用户
名称
端口
状态
)可能是MySQL关键字。试着用庄重的口音(`)(我也将它们添加到了我的帖子中)。

您必须删除
ORDER BY
子句中
user
的引号。这就是导致“
ORDER BY
无法按预期工作的原因,因为您可以在“
ORDER BY
子句”中使用任何表达式,并且引号中的“
”user“
被视为表达式(常量),而不是列名

测试用例(MySQL 5.1.45):


或者更好的是重新命名它们。像那样的名字使我十分恼火。谢谢你,我已经更新了我的帖子@乐多菲;你完全正确。修复了代码问题(复制粘贴错误)。带引号的解决方案正在运行!有趣的是,看起来v。4.1.13使用引号,但v 5。没有。是的,那很有趣。但是,我建议不要在列名周围使用引号。您可以改为使用反勾号:
user
CREATE TABLE tb (id int);

INSERT INTO tb VALUES (5);
INSERT INTO tb VALUES (1);
INSERT INTO tb VALUES (4);
INSERT INTO tb VALUES (2);
INSERT INTO tb VALUES (3);

SELECT * FROM tb ORDER BY 'id';
+------+
| id   |
+------+
|    5 |
|    1 |
|    4 |
|    2 |
|    3 |
+------+
5 rows in set (0.00 sec)

SELECT * FROM tb ORDER BY id;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
|    5 |
+------+
5 rows in set (0.00 sec)