有什么办法可以将这两个查询合二为一吗?
Is there any way to combine these two queries into one?
我正在创建一个测验应用程序,对于主页,我希望用户能够看到他们玩过的测验以及管理员和他们自己编写的测验。但是,我正在努力将这些组合成一个查询。我试过加入,但是因为我在第二个查询中使用了 where 子句,所以我无法加入它们。任何帮助将不胜感激。谢谢!
select quiz.title, quiz.quizid
from quiz
where id =
or id = 'admin'
order by quiz.id desc
这个returns某个id和admin写的所有值
select distinct quiz.quizid,quiz.title
from quiz
inner join played on quiz.quizid = played.quizid
此查询returns用户玩过的所有小测验
在我看来你需要玩 LEFT JOIN 而不是 INNER JOIN
使用左连接意味着 played.quizid
对于尚未玩过的测验将为空,这意味着您可以在 select..
注意不要在 WHERE
子句中使用来自 played
的任何列,因为它会导致 LEFT JOIN 恢复为 INNER(您将要求数据库比较 NULL到某事,这最终总是错误的)
SELECT *
FROM L LEFT JOIN R ON L.id = R.id
WHERE R.somecolmumn = 'somevalue' --no; it will discard the NULLs the left join introduces
要么保留空值,要么排除 ON 中的限制:
SELECT *
FROM L LEFT JOIN R ON L.id = R.id AND R.somecolmumn = 'somevalue' --yes
SELECT *
FROM L LEFT JOIN R ON L.id = R.id
WHERE
(R.somecolmumn = 'somevalue' OR R.somecolumn IS NULL) --yes, but only if somecolumn doesn't naturally contain NULL
根据评论编辑:
好的,所以解决这个问题的最简单方法可能是两部分,因为进行的测验可能是他们自己的、管理员的或一些随机用户的..
SELECT quizid FROM quiz WHERE author_userid = OR author_userid = 'ADMIN'
进行他们自己的测验和管理员测验。
SELECT DISTINCT quizid FROM played WHERE player_userid =
获取他们玩过的测验列表。您可以将它们与 UNION 放在一起:
SELECT quizid FROM quiz WHERE author_userid = OR author_userid = 'ADMIN'
UNION
SELECT DISTINCT quizid FROM played WHERE player_userid =
这是一列唯一的测验 ID。您可以再次加入小测验了解详情:
SELECT *
FROM
(
SELECT quizid FROM quiz WHERE author_userid = OR author_userid = 'ADMIN'
UNION
SELECT DISTINCT quizid FROM played WHERE player_userid =
) show
INNER JOIN quiz ON quiz.quizid = show.quizid
但是你确实失去了“它在那里的原因”
如果您将所有测验连接在一起以左连接不同的游戏:
SELECT *
FROM
quiz
LEFT JOIN (SELECT DISTINCT quizid FROM played WHERE played_userid = ) plyd
ON quiz.quizid = plyd.quizid
WHERE
quiz.author_userid IN ('ADMIN', ) --admin's or their own
OR
plyd.quizid IS NOT NULL --other ppl's quizzes they played
您可以结合查看测验的作者和 plyd.quizid 是否为 null(null = 他们没有玩过,not null = 他们玩过)来找出为什么存在测验它)
尝试使用 UNION
select quiz.quizid, quiz.title, 1 as query
from quiz
where id =
or id = 'admin'
UNION
select distinct quiz.quizid,quiz.title, 2 as query
from quiz
inner join played on quiz.quizid = played.quizid
我正在创建一个测验应用程序,对于主页,我希望用户能够看到他们玩过的测验以及管理员和他们自己编写的测验。但是,我正在努力将这些组合成一个查询。我试过加入,但是因为我在第二个查询中使用了 where 子句,所以我无法加入它们。任何帮助将不胜感激。谢谢!
select quiz.title, quiz.quizid
from quiz
where id =
or id = 'admin'
order by quiz.id desc
这个returns某个id和admin写的所有值
select distinct quiz.quizid,quiz.title
from quiz
inner join played on quiz.quizid = played.quizid
此查询returns用户玩过的所有小测验
在我看来你需要玩 LEFT JOIN 而不是 INNER JOIN
使用左连接意味着 played.quizid
对于尚未玩过的测验将为空,这意味着您可以在 select..
注意不要在 WHERE
子句中使用来自 played
的任何列,因为它会导致 LEFT JOIN 恢复为 INNER(您将要求数据库比较 NULL到某事,这最终总是错误的)
SELECT *
FROM L LEFT JOIN R ON L.id = R.id
WHERE R.somecolmumn = 'somevalue' --no; it will discard the NULLs the left join introduces
要么保留空值,要么排除 ON 中的限制:
SELECT *
FROM L LEFT JOIN R ON L.id = R.id AND R.somecolmumn = 'somevalue' --yes
SELECT *
FROM L LEFT JOIN R ON L.id = R.id
WHERE
(R.somecolmumn = 'somevalue' OR R.somecolumn IS NULL) --yes, but only if somecolumn doesn't naturally contain NULL
根据评论编辑:
好的,所以解决这个问题的最简单方法可能是两部分,因为进行的测验可能是他们自己的、管理员的或一些随机用户的..
SELECT quizid FROM quiz WHERE author_userid = OR author_userid = 'ADMIN'
进行他们自己的测验和管理员测验。
SELECT DISTINCT quizid FROM played WHERE player_userid =
获取他们玩过的测验列表。您可以将它们与 UNION 放在一起:
SELECT quizid FROM quiz WHERE author_userid = OR author_userid = 'ADMIN'
UNION
SELECT DISTINCT quizid FROM played WHERE player_userid =
这是一列唯一的测验 ID。您可以再次加入小测验了解详情:
SELECT *
FROM
(
SELECT quizid FROM quiz WHERE author_userid = OR author_userid = 'ADMIN'
UNION
SELECT DISTINCT quizid FROM played WHERE player_userid =
) show
INNER JOIN quiz ON quiz.quizid = show.quizid
但是你确实失去了“它在那里的原因”
如果您将所有测验连接在一起以左连接不同的游戏:
SELECT *
FROM
quiz
LEFT JOIN (SELECT DISTINCT quizid FROM played WHERE played_userid = ) plyd
ON quiz.quizid = plyd.quizid
WHERE
quiz.author_userid IN ('ADMIN', ) --admin's or their own
OR
plyd.quizid IS NOT NULL --other ppl's quizzes they played
您可以结合查看测验的作者和 plyd.quizid 是否为 null(null = 他们没有玩过,not null = 他们玩过)来找出为什么存在测验它)
尝试使用 UNION
select quiz.quizid, quiz.title, 1 as query
from quiz
where id =
or id = 'admin'
UNION
select distinct quiz.quizid,quiz.title, 2 as query
from quiz
inner join played on quiz.quizid = played.quizid