问题
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