0

我想选择条件为真的行。我有一个名为 的列command,如果列值包含(由我定义的)字符串,则添加到结果中。

我的问题是选择行,因为查询没有看到 WHERE 语句,如果列command包含字符串,它会选择所有数据。

SELECT * 
FROM `admin_log` 
WHERE `admin_log`.`user` = 'Admin' 
    AND command LIKE '%aznot%' 
    OR command LIKE '%hango%' 
ORDER BY `order_id` DESC 
LIMIT 0, 50

例如,我有 3 个管理员帐户:Admin、Zuppa、Grund。如果命令列包含:aznot 或 hango,我想按帐户 ADMIN 选择所有数据。

我究竟做错了什么?

4

6 回答 6

2

你需要一些括号。

SELECT * 
FROM `admin_log`
WHERE `admin_log`.`user` = 'Admin'  
AND (command LIKE '%aznot%' OR command LIKE '%hango%')
ORDER BY `order_id` 
DESC LIMIT 0, 50
于 2012-06-13T12:50:27.817 回答
1
SELECT * FROM `admin_log` WHERE `admin_log`.`user` = 'Admin' 
 AND (command LIKE '%aznot%' OR command LIKE '%hango%') 
 ORDER BY `order_id` DESC LIMIT 0, 50
于 2012-06-13T12:50:35.597 回答
1

尝试这样的事情:

SELECT * FROM `admin_log` WHERE `admin_log`.`user` = 'Admin' AND (command LIKE '%aznot%' OR command LIKE '%hango%') ORDER BY `order_id` DESC LIMIT 0, 50
于 2012-06-13T12:50:42.510 回答
1

更好的做法是将 OR 语句分成单独的表达式,以防止引擎误解。

SELECT * FROM `admin_log` WHERE `admin_log`.`user` = 'Admin' AND (command LIKE '%aznot%' OR command LIKE '%hango%') ORDER BY `order_id` DESC LIMIT 0, 50
于 2012-06-13T12:51:07.500 回答
1
SELECT * 
FROM   `admin_log` 
WHERE  `admin_log`.`user` = 'Admin' 
       AND (command LIKE '%aznot%'
        OR command LIKE '%hango%' )
ORDER  BY `order_id` DESC 
LIMIT  0, 50 
于 2012-06-13T12:51:27.647 回答
1

您需要正确嵌套条件。查看 SQL 中逻辑运算符的优先规则

于 2012-06-13T12:51:31.400 回答