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

标签 mysql sql

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

scores
    scoreId int not null,
    comments varchar(128)

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

我想获取符合以下条件的评论和 contentId 的值...

评论!=“” 或者 ScoreId 存在 contentId

这是表值...

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;

如果评论或媒体评论具有值,我会尝试获取结果。如果值与查询不匹配,我希望返回 NULL。例如,如果在 mediaComments 中找不到 s.scoreId,但 s.comments 有一个值,我希望将 m.contentId 返回为 NULL。

这是我得到的结果...

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)

最佳答案

这是你想要的吗?

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;

关于如果任一匹配查询,MySQL 查询将返回 2 个值的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55560573/

相关文章:

mysql - 如何处理 nodejs 与 mysql 的异步?

mysql - Rails 范围 null 返回错误结果

将 Excel 日期数据保存到 MySQL 时,Java 日期格式化程序不起作用

mysql - 使用 DATE_SUB 选择小于 5 分钟的行

mysql - SQL:根据两列的串联获取唯一行

SQL 选择其中只有特定值的行

python - SQLAlchemy 删除级联完整性错误

sql - 从 oracle 过程返回 Json 字符串

mysql - 如何从 SQL 中的相关表中获取单个结果?

mysql - 如果表中不存在另一列则按一列排序的 SQL 语句