gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 16:07:49 24 4
gpt4 key购买 nike

我想查询 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com