Mysql选择语句

tma*_*314 0 mysql sql

我需要一种正确的方法来编写一个简单的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
Run Code Online (Sandbox Code Playgroud)

OMG*_*ies 5

这是一个有效的陈述,但我想问题是OR并没有像你期望的那样被解释.请改用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
Run Code Online (Sandbox Code Playgroud)

我删除了某些位置的反引号,因为它们只是用于转义使用MySQL保留关键字的表和列名称所必需的.

这样的方式:

WHERE `type` = 'wall_common_text' 
   OR `type` = 'wall_common_fb' 
   OR `type`= 'wall_common_tw' 
  AND `owner_id`='{$iId}' 
Run Code Online (Sandbox Code Playgroud)

...正在评估的是:

WHERE (`type` = 'wall_common_text')
   OR (`type` = 'wall_common_fb')
   OR (`type`= 'wall_common_tw' AND `owner_id`='{$iId}')
Run Code Online (Sandbox Code Playgroud)