如果任一查询匹配,MySQL查询将返回两个值的结果

时间:2019-04-07 15:23:53

标签: mysql sql

我想查询2个表以查找匹配值,并在任一条件匹配时返回结果。这些表仅显示相关列...

scores
    scoreId int not null,
    comments varchar(128)

mediaComments
    contentId varchar(40) not null,
    scoreId int (foreign key to scores.scoreId)

我想获取以下条件的comment和contentId值...

评论!=“ 要么 contentId存在于scoreId

这是表格值...

mysql> select * from mediaComments;
+---------+-----------------------------+
| scoreId | contentId                   |
+---------+-----------------------------+
|       1 | CON-i6FYCiNDJ0u7mbGfttK8Joy |
|       2 | CON-RgNzMie5F8EdSnEq7122siV |
|       4 | CON-3bo1iGIBdu623TS4ltggytT |
|       6 | CON-qjMVn2THP6d2nCta9JWL1na |
+---------+-----------------------------+
4 rows in set (0.00 sec)

mysql> select scoreId, comments from scores;
+---------+---------------------------+
| scoreId | comments                  |
+---------+---------------------------+
|       1 |                           |
|       2 | ReadScoreCommentsMethods1 |
|       3 | ReadScoreCommentsMethods2 |
|       4 | ReadScoreCommentsMethods3 |
|       5 | ReadScoreCommentsMethods4 |
|       6 | ReadScoreCommentsMethods5 |
|       7 |                           |
|       8 |                           |
|       9 |                           |
+---------+---------------------------+
9 rows in set (0.00 sec)

这是我正在使用的简单查询。

select
    s.scoreId,
    s.comments,
    m.contentId
from
    scores s,
    mediaComments m
where
    s.comments != '' or
    m.scoreId = s.scoreId;

如果注释或mediaComments有值,我试图获得结果。如果值与查询不匹配,我想返回NULL。例如,如果在mediaComments中找不到s.scoreId,但我想让m.contentId作为NULL返回,但是s.comments有一个值。

这是我得到的结果...

mysql> select
    ->     s.scoreId,
    ->     s.comments,
    ->     m.contentId
    -> from
    ->     scores s,
    ->     mediaComments m
    -> where
    ->     s.comments != '' or
    ->     m.scoreId = s.scoreId;
+---------+---------------------------+-----------------------------+
| scoreId | comments                  | contentId                   |
+---------+---------------------------+-----------------------------+
|       1 |                           | CON-i6FYCiNDJ0u7mbGfttK8Joy |
|       2 | ReadScoreCommentsMethods1 | CON-i6FYCiNDJ0u7mbGfttK8Joy |
|       2 | ReadScoreCommentsMethods1 | CON-RgNzMie5F8EdSnEq7122siV |
|       2 | ReadScoreCommentsMethods1 | CON-3bo1iGIBdu623TS4ltggytT |
|       2 | ReadScoreCommentsMethods1 | CON-qjMVn2THP6d2nCta9JWL1na |
|       3 | ReadScoreCommentsMethods2 | CON-i6FYCiNDJ0u7mbGfttK8Joy |
|       3 | ReadScoreCommentsMethods2 | CON-RgNzMie5F8EdSnEq7122siV |
|       3 | ReadScoreCommentsMethods2 | CON-3bo1iGIBdu623TS4ltggytT |
|       3 | ReadScoreCommentsMethods2 | CON-qjMVn2THP6d2nCta9JWL1na |
|       4 | ReadScoreCommentsMethods3 | CON-i6FYCiNDJ0u7mbGfttK8Joy |
|       4 | ReadScoreCommentsMethods3 | CON-RgNzMie5F8EdSnEq7122siV |
|       4 | ReadScoreCommentsMethods3 | CON-3bo1iGIBdu623TS4ltggytT |
|       4 | ReadScoreCommentsMethods3 | CON-qjMVn2THP6d2nCta9JWL1na |
|       5 | ReadScoreCommentsMethods4 | CON-i6FYCiNDJ0u7mbGfttK8Joy |
|       5 | ReadScoreCommentsMethods4 | CON-RgNzMie5F8EdSnEq7122siV |
|       5 | ReadScoreCommentsMethods4 | CON-3bo1iGIBdu623TS4ltggytT |
|       5 | ReadScoreCommentsMethods4 | CON-qjMVn2THP6d2nCta9JWL1na |
|       6 | ReadScoreCommentsMethods5 | CON-i6FYCiNDJ0u7mbGfttK8Joy |
|       6 | ReadScoreCommentsMethods5 | CON-RgNzMie5F8EdSnEq7122siV |
|       6 | ReadScoreCommentsMethods5 | CON-3bo1iGIBdu623TS4ltggytT |
|       6 | ReadScoreCommentsMethods5 | CON-qjMVn2THP6d2nCta9JWL1na |
+---------+---------------------------+-----------------------------+
21 rows in set (0.00 sec)

2 个答案:

答案 0 :(得分:0)

这是您想要的吗?

select s.*
from scores s
where s.comment <> '' or
      exists (select 1
              from mediaComments mc
              where mc.scoreid = s.scoreid
             );

如果您也想要content_id,则类似以下内容:

select s.*, mc.contentid
from scores s left join
     mediaComments mc
     on mc.scoreid = s.scoreid
where s.comment <> null or mc.contentid is not null;

答案 1 :(得分:0)

不清楚您要的是什么。 但是,您可能应该使用OUTER JOIN。 FULL OUTER JOIN返回两个表中的所有内容,无论是否为空。

SELECT
    s.scoreId,
    s.comments,
    m.contentId
FROM
    scores as s 
    FULL OUTER JOIN mediaComments as m 
    ON s.scoreID = m.contentId;
```mysql