使用MySQL php选择除三个以外的所有行

时间:2017-10-26 10:55:08

标签: php mysql select sql-order-by where-clause

我试图使用select语句来获取某个MySQL表中的所有行,除了在user_id为5,6,7的三个中。下面是代码,但它无法正常工作。请帮忙。

$sql = "SELECT * FROM login ORDER BY user_id ASC LIMIT 0, 20 WHERE user_id<>5,6,7";

4 个答案:

答案 0 :(得分:3)

您必须对多个ID使用 NOT IN 功能。

$sql = "SELECT * FROM login WHERE user_id NOT IN (5,6,7) ORDER BY user_id ASC LIMIT 0, 20"

答案 1 :(得分:2)

$sql = "SELECT * FROM login WHERE user_id<>5 ORDER BY user_id ASC LIMIT 0, 20 "

查看Order byWhere ClauseOrder of operations

正如您刚刚更改了您的问题,新答案是

$sql = "SELECT * FROM login WHERE user_id NOT IN (5,6,7) ORDER BY user_id ASC LIMIT 0, 20 "

查看NOT IN

答案 2 :(得分:2)

这样做:

 SELECT * FROM login WHERE NOT user_id = 5, ORDER BY user_id ASC LIMIT 0, 20

答案 3 :(得分:-1)

试试这个:

$sql = "SELECT * FROM `login` WHERE `user_id` NOT IN (5,6,7) ORDER BY `user_id` ASC LIMIT 0, 20";