[英]How to replace outer query value if subquery returns null
This is my query: 这是我的查询:
SELECT * FROM main_menu AS m WHERE EXISTS(select * from menu where menu.main_menu_id = m.main_menu_id AND menu.menu IS NOT NULL)
Above query shows rows from main_menu table only if their respective rows in menu table is not NULL, which is correct. 上面的查询仅在菜单表中的相应行不为NULL时显示main_menu表中的行,这是正确的。
But I would like to do something more. 但我想做更多的事情。 If the value for menu table(subquery) found NULL , the main_menu value(outer query) should be altered,something like using
IFNULL
but for the outer query. 如果菜单表(子查询)的值找到NULL,则应更改
IFNULL
值(外部查询),类似于使用IFNULL
但用于外部查询。 How do I do that, please? 请问我该怎么做?
Then you should use a LEFT JOIN
and not EXISTS()
statement : 然后,您应该使用
LEFT JOIN
而不是EXISTS()
语句:
SELECT m.*,COALESCE(m2.<Column>,OtherValue),....
FROM main_menu m
LEFT JOIN menu m2
ON(m2.main_menu_id = m.maid_menu_id AND m2.menu is not null)
This will join both tables even when there is no such record, and will have NULL
values for all menu columns. 即使没有这样的记录,这也将联接两个表,并且所有菜单列的值都为
NULL
。
You didn't define 'should be altered' , so that's the best I can come up with. 您没有定义“应该更改”,所以这是我能想到的最好的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.