douaonong7807 2013-08-05 03:34
浏览 35

使用PHP和MYSQL返回具有特定ID号的行?

I've been having difficulty returning the correct rows with my SQL query. I want to only return rows with either 31 or 34 as their album_items.id. I want to exclude any other album_items.id from my results. The main line I'm focused on is:

WHERE album_items.album IN (31,34) 

For whatever reason, even the rows that don't have album_items.id of 31 or 34 are still being returned. Here's the full query. Is it possible that I haven't used to right syntax?

SELECT * FROM items_table 
RIGHT JOIN items_table 
ON items_table.id=album_items.id 
WHERE album_items.album IN (31,34)
AND items_table.name LIKE '%{$term}%' 
OR items_table.description LIKE '%{$term}%'
AND items_table.active != '0'

Thanks for your time, Sarah

  • 写回答

3条回答 默认 最新

  • dtk31564 2013-08-05 03:39
    关注

    I believe this is a problem with the order of operations.

    In you sql, you have

    WHERE album_items.album IN (31,34)
    AND items_table.name LIKE '%{$term}%' 
    OR items_table.description LIKE '%{$term}%'
    AND items_table.active != '0'
    

    Which is interpreted as

    WHERE 
    (album_items.album IN (31,34) AND items_table.name LIKE '%{$term}%')
    OR
    (items_table.description LIKE '%{$term}%' AND items_table.active != '0')
    

    Most likely, the second condition is causing the extra rows. You'll want to include parenthesis where appropriate.

    评论

报告相同问题?