开发者

Mysql select statement

I need a proper way to write a simple WHERE, AND, OR statement. This one doesn't work:

SELECT `content` 
  FROM `bx_wall_events` 
 开发者_StackOverflowWHERE `type` = 'wall_common_text' 
    OR `type` = 'wall_common_fb' 
    OR `type`= 'wall_common_tw' 
   AND `owner_id`='{$iId}' 
ORDER BY `date` DESC 
   LIMIT 1


It's a valid statement, but I imagine the issue is the ORs aren't being interpreted like you'd expect. Use the IN syntax instead:

  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

I removed the backticks in certain spots because they're only necessary for escaping table & column names that are using MySQL reserved keywords.

The way this:

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

...is evaluating is:

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 ((type= 'wall_common_text') || (type= 'wall_common_fb') || (type= 'wall_common_tw')) AND owner_id='{$iId}' ORDER BY date DESC LIMIT 1"


Found a great solution using the IN

"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"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜