Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql select语句_Sql_Mysql - Fatal编程技术网

Mysql select语句

Mysql select语句,sql,mysql,Sql,Mysql,我需要一个合适的方法来写一个简单的WHERE,AND,OR语句。这个不行: SELECT `content` FROM `bx_wall_events` WHERE `type` = 'wall_common_text' OR `type` = 'wall_common_fb' OR `type`= 'wall_common_tw' AND `owner_id`='{$iId}' ORDER BY `date` DESC LIMIT 1 “从bx

我需要一个合适的方法来写一个简单的WHERE,AND,OR语句。这个不行:

SELECT `content` 
  FROM `bx_wall_events` 
 WHERE `type` = 'wall_common_text' 
    OR `type` = 'wall_common_fb' 
    OR `type`= 'wall_common_tw' 
   AND `owner_id`='{$iId}' 
ORDER BY `date` DESC 
   LIMIT 1
“从
bx\u wall\u事件中选择
content
其中(
类型
='wall\u common\u text')|(
类型
='wall\u common\u fb')|(
类型
='wall\u common\u tw')) 和
owner\u id
='{$iId}'订单按
date
DESC LIMIT 1

bx\u wall\u事件中选择
content
其中(
类型
='wall\u common\u text')|(
类型
='wall\u common\u fb')|(
类型
='wall\u common\u tw'))
owner\u id
='{$iId}'按
date
DESC LIMIT 1]排序这是一个有效的声明,但我想问题是ORs没有像您预期的那样被解释。请改用IN语法:

  SELECT content
    FROM bx_wall_events
   WHERE `type` IN ('wall_common_text', 'wall_common_fb', 'wall_common_tw')
     AND `owner_id` = '{$iId}' 
ORDER BY `date` DESC 
   LIMIT 1
我删除了某些点中的反勾号,因为它们仅用于转义正在使用的表名和列名

这种方式:

WHERE `type` = 'wall_common_text' 
   OR `type` = 'wall_common_fb' 
   OR `type`= 'wall_common_tw' 
  AND `owner_id`='{$iId}' 
…正在评估的是:

WHERE (`type` = 'wall_common_text')
   OR (`type` = 'wall_common_fb')
   OR (`type`= 'wall_common_tw' AND `owner_id`='{$iId}')

这是一个有效的说法,但我想问题在于ORs没有像你预期的那样被解释。请改用IN语法:

  SELECT content
    FROM bx_wall_events
   WHERE `type` IN ('wall_common_text', 'wall_common_fb', 'wall_common_tw')
     AND `owner_id` = '{$iId}' 
ORDER BY `date` DESC 
   LIMIT 1
我删除了某些点中的反勾号,因为它们仅用于转义正在使用的表名和列名

这种方式:

WHERE `type` = 'wall_common_text' 
   OR `type` = 'wall_common_fb' 
   OR `type`= 'wall_common_tw' 
  AND `owner_id`='{$iId}' 
…正在评估的是:

WHERE (`type` = 'wall_common_text')
   OR (`type` = 'wall_common_fb')
   OR (`type`= 'wall_common_tw' AND `owner_id`='{$iId}')

找到了一个很好的解决方案,使用

"SELECT `content` 
FROM `bx_wall_events` 
WHERE `owner_id`='{$iId}' 
AND `type`
IN ('wall_common_text', 'wall_common_fb', 'wall_common_tw') 
ORDER BY `date` 
DESC LIMIT 1"

找到了一个很好的解决方案,使用

"SELECT `content` 
FROM `bx_wall_events` 
WHERE `owner_id`='{$iId}' 
AND `type`
IN ('wall_common_text', 'wall_common_fb', 'wall_common_tw') 
ORDER BY `date` 
DESC LIMIT 1"

您也可以在ORs周围放上括号,将它们与AND分开,但我同意这是一种方法。@Michael Madsen:可以,但
中的
更易于维护--缺少或放错一个括号,查询可能有效,但不会返回正确的数据。如果在
中的
中缺少一个括号,您肯定会有语法错误。您也可以在or周围放上括号,将它们与and分开,但我同意这是解决问题的方法。@Michael Madsen:可以,但是
中的
更易于维护--缺少或放错一个括号,查询可能有效,但不会返回正确的数据。如果在
中的
中缺少括号,则保证出现语法错误。在MySQL中,默认情况下,| |是逻辑OR运算符。启用了PIPES_AS_CONCAT,|是字符串连接(请参阅)@martin clayton:我不知道,但我知道如果启用严格的SQL,则双管道用于字符串连接…在MySQL中,默认情况下,|是逻辑OR运算符。启用了PIPES_AS_CONCAT,| |是字符串连接(请参阅)@martin clayton:我不知道这一点,但我知道如果启用严格SQL,则双管道用于字符串连接。。。