dongyi8383 2014-02-20 14:17
浏览 382
已采纳

我应该在将mysql查询与三个IN语句或其他内容结合使用时使用AND吗?

Im trying to combine 3 IN statements with ANd in a search im working on. Here is and example that doesnt fully works as i need it to be

SELECT name,thumbnail
 FROM table1 WHERE
       id IN (SELECT imgid FROM categories WHERE category = 22 )
   AND id IN (SELECT imgid FROM namesearch WHERE name LIKE "%car%" )
   OR id IN (SELECT imgid FROM tags WHERE tag LIKE "%jet%" )

The above example logically it is in a group like this if im not wrong

(categorie=22 AND tags=car) OR (namesearch=jet)

And if i search jet that is found at tag it doesnt filter category=22

How can i group the statements in a logic like this

(categorie=22) AND (tags=car OR|AND namesearch=jet)

It filters category if i put an AND like this

SELECT name,thumbnail
 FROM table1 WHERE
       id IN (SELECT imgid FROM categories WHERE category = 22 )
   AND id IN (SELECT imgid FROM namesearch WHERE name LIKE "%car%" )
   AND id IN (SELECT imgid FROM tags WHERE tag LIKE "%jet%" )


But will it search in namesearch or tags or at both in the same time?

Because i dont want to miss tag search if a namesearch is found, i need them both, but namesearch is in high priority.

  • 写回答

1条回答 默认 最新

  • dongying6179 2014-02-20 14:19
    关注

    Is this what you want?

    SELECT name, thumbnail
    FROM table1
    WHERE id IN (SELECT imgid FROM categories WHERE category = 22 ) AND
          (id IN (SELECT imgid FROM namesearch WHERE name LIKE '%car%' ) OR
           id IN (SELECT imgid FROM tags WHERE tag LIKE '%jet%' )
          )
    

    Notice that I used single quotes for the string constants. This is good practice.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗