mysql not in or value=0?

南笙酒味 提交于 2019-12-13 03:02:01

问题


Database one is called widgets, it has "id" and "title". Database two is called widget-layouts and it has "module-id", "widget-id", "position", and "weight".

What I am trying to do is check to see if widgets.id exists in widget-layout.widget-id and if it does, then does widget-layouts.position = 0. I also want to get the values of widgets that don't exist in widget-layouts.

Here is the mysql query I have been working with.

SELECT * FROM widgets, widget-layouts WHERE (widge-layouts.position = '0' AND widgets.id = widget-layouts.widget-id) OR widgets.id NOT IN (SELECT * FROM widget-layouts)

With this query I am getting a huge list of widgets where each widget is displayed multiple times.

Any ideas about this?


回答1:


 SELECT *
 FROM widgets AS w
     LEFT JOIN widget-layouts AS wl
         ON w.id = wl.widget-id
 WHERE wl.widget-id IS NULL
     OR wl.position = '0'      



回答2:


SELECT * FROM widgets LEFT JOIN widget-layouts on widget-id = id WHERE position = 0

should show those that exist and have position = 0

SELECT * FROM widgets RIGHT JOIN widget-layouts on id = widget-id where id is NULL

should show those that exist in widget-layouts only



来源:https://stackoverflow.com/questions/1940228/mysql-not-in-or-value-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!