MySQL不在或值= 0? [英] mysql not in or value=0?
问题描述
一个数据库称为小部件,它具有"id"和"title".数据库2称为小部件布局,它具有模块ID",小部件ID",位置"和重量".
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".
我想做的是检查widget-layout.widget-id中是否存在widgets.id,如果存在,那么widget-layouts.position =0.我还想获取widgets的值在小部件布局中不存在.
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.
这是我一直在使用的mysql查询.
Here is the mysql query I have been working with.
选择*从小部件,小部件布局 在哪里(widge-layouts.position ='0' AND widgets.id = widget-layouts.widget-id)或 widgets.id不在(选择*从 小部件布局)
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.
对此有什么想法吗?
推荐答案
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'
这篇关于MySQL不在或值= 0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!