douzhulv1699 2017-05-22 19:23
浏览 25
已采纳

使用LIKE检索行并传递一些id。

I want to retrieve a row with keyword "off" and sc_id must be equal to "2". I am trying following query but it gives me the rows with keyword "Off" and also gives all the sc_id's. How to solve this. The id search is the must part.

Query

SELECT c.* , sc.* , sm.* ,ca.* 
from store_category sc 
INNER JOIN store_manufacture sm 
   ON sm.sm_id=sc.store_id 
INNER JOIN categories ca 
   ON ca.cat_id=sc.cat_id 
INNER JOIN coupons c 
   on c.c_sc_id=sc.sc_id 
WHERE sc.sc_id=2 
  AND c.c_name LIKE '%off%' 
   OR sm.sm_brand_name LIKE '%off%' 
   OR ca.cat_name LIKE '%off%' 
   OR c.c_description LIKE '%off%'
  • 写回答

1条回答 默认 最新

  • dongleiwei2182 2017-05-22 19:48
    关注

    You need to use parenthesis for your conditions, try this:

    SELECT c.* , sc.* , sm.* ,ca.* 
    from store_category sc 
    INNER JOIN store_manufacture sm 
    ON sm.sm_id=sc.store_id 
    INNER JOIN categories ca 
    ON ca.cat_id=sc.cat_id 
    INNER JOIN coupons c 
    ON c.c_sc_id=sc.sc_id 
    WHERE sc.sc_id=2 
    AND (c.c_name LIKE '%off%' 
    OR sm.sm_brand_name LIKE '%off%' 
    OR ca.cat_name LIKE '%off%' 
    OR c.c_description LIKE '%off%');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路