如何处理 If a subquery (inner query) returns a null value to outer query

how to handle If a subquery (inner query) returns a null value to the outer query

我有一个像这样的垂直 table:

id Profile_id feature_id value
1 1 1 Rick
2 1 2 Novak
3 5 3 5428
4 5 1 Joe
... ... ... ...

(以上table是profile_featurestable的短部分)

我有一个问题:

SELECT * FROM(SELECT `value` TelNum FROM `profile_features` WHERE `feature_id` IN (10, 64, 103) AND `profile_id` = 16752 LIMIT 1) as TelNum,
    (SELECT `value` NCode From `profile_features` WHERE `feature_id` IN (5, 61, 100) AND `profile_id` = 16752 LIMIT 1)  AS NCode,
    (SELECT `value` Fname From `profile_features` WHERE `feature_id` IN (1, 55, 86) AND `profile_id` = 16752 LIMIT 1) AS Fname,
    (SELECT `value` Lname From `profile_features` WHERE `feature_id` IN (2, 56, 95) AND `profile_id` = 16752 LIMIT 1) AS Lname

但是如果其中一个子查询returns为null,则查询没有任何输出。

我想要一个包含 TelNum、NCode、Fname、Lname 的行结果,它们要么为 Null,要么不为 null。

您可以在 select-list 中使用标量子查询而不是当前的笛卡尔积:

SELECT (SELECT `value` FROM `profile_features` WHERE `feature_id` IN (10, 64, 103) AND `profile_id` = 16752 LIMIT 1) as TelNum,
  (SELECT `value` From `profile_features` WHERE `feature_id` IN (5, 61, 100) AND `profile_id` = 16752 LIMIT 1)  AS NCode,
  (SELECT `value` From `profile_features` WHERE `feature_id` IN (1, 55, 86) AND `profile_id` = 16752 LIMIT 1) AS Fname,
  (SELECT `value` From `profile_features` WHERE `feature_id` IN (2, 56, 95) AND `profile_id` = 16752 LIMIT 1) AS Lname;

或者,您可以学习使用 LEFT OUTER JOIN:

SELECT TelNum.TelNum, NCode.NCode, Fname.Fname, Lname.Lname
FROM (SELECT NULL AS dummy) AS dummy
LEFT OUTER JOIN (SELECT `value` TelNum FROM `profile_features` WHERE `feature_id` IN (10, 64, 103) AND `profile_id` = 16752 LIMIT 1) AS TelNum ON true
LEFT OUTER JOIN (SELECT `value` NCode From `profile_features` WHERE `feature_id` IN (5, 61, 100) AND `profile_id` = 16752 LIMIT 1) AS NCode ON true
LEFT OUTER JOIN (SELECT `value` Fname From `profile_features` WHERE `feature_id` IN (1, 55, 86) AND `profile_id` = 16752 LIMIT 1) AS Fname ON true
LEFT OUTER JOIN (SELECT `value` Lname From `profile_features` WHERE `feature_id` IN (2, 56, 95) AND `profile_id` = 16752 LIMIT 1) AS Lname ON true;