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.

    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数