开发者

Mysql select statement

开发者 https://www.devze.com 2023-01-15 20:05 出处:网络
I need a proper way to write a simple WHERE, AND, OR statement.This one doesn\'t work: SELECT `content`

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

精彩评论

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