MySQL 连接,查询返回所有章节标题以及用户的等级(如果有)
我有这样的表:
[users] [chapters] [exam_scores]
- id - title - fk_chapter
- fk_user
- grade
我需要一个查询来返回所有章节标题以及可用的用户等级。这是我的尝试:
select chapters.title as Title, exam_scores.grade as Results
from users, exam_scores left join chapters
on chapters.id = exam_scores.fk_chapter
where exam_scores.fk_user = users.id and users.id = 15;
如果有可用的成绩,则返回记录,但不返回其余章节。
I have my tables like that:
[users] [chapters] [exam_scores]
- id - title - fk_chapter
- fk_user
- grade
I need a query to return all the chapter titles and where available the grade of the user. Here is my try:
select chapters.title as Title, exam_scores.grade as Results
from users, exam_scores left join chapters
on chapters.id = exam_scores.fk_chapter
where exam_scores.fk_user = users.id and users.id = 15;
That one returns records if there is available grade, but not the rest of the chapters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要
LEFT JOIN
两次才能完成此操作。顺便说一句,我还建议避免使用“旧式”连接语法(FROM table1、table2、table3 ...),因为它太混乱了这些类型的连接。You'll need to
LEFT JOIN
twice in order to accomplish this. As an aside, I'd also recommend avoiding the 'old style' join syntax (FROM table1, table2, table3 ...) as it's all too mess up these types of join.试试这个:
Try This:
是的,左连接两次:
Yep, left join twice:
由于您想要获取所有章节的列表,无论该章节是否有用户评分。因此,您需要从章节表中进行 SELECT。然后,由于您想要每个章节的结果,因此需要 LEFT JOIN exam_scores 表,以便您将获得有分数的行的记录,以及没有分数的行的记录。使用自然连接(
JOIN
或FROM table, table2, table3
)时,仅返回左值和右值匹配的记录。因此,您的查询应如下所示:
请注意,您甚至不需要在此处包含
users
表,因为您没有访问它的任何信息。您的查询是加入 users 表,然后对其id
键执行WHERE
子句。这不是必需的,除非您从用户表中获取数据。事实上,如果您确实需要用户表中的数据,则可以将其左连接到 exam_scores 表上,如下所示:希望这对您将来应该如何更好地构建查询和连接有所帮助并提供一些启示。
编辑:我编辑了我的答案以回应您的评论。这是完全有道理的,为什么您不会在没有附加成绩的情况下收到回复,原因如下:
当您将两个表连接在一起时,它们将通过用户 ID 连接起来。没关系,因为当用户 ID 未映射到章节表时,您会得到用户 ID 和等级的
NULL
值。但是,由于在WHERE
子句中,我们正在过滤 ID 为15
的用户,因此它会过滤掉用户列的所有NULL
值。因此,为了解决这个问题,您可以在WHERE
子句中添加一个OR
语句,表示用户 ID 为NULL
是可以的。Since you want to get a list of all the chapters, regardless of whether or not there is a user score for the chapter. Because of that, you want to SELECT from the chapters table. Then, since you want a result for each chapter, you need to LEFT JOIN the exam_scores table so you will get records for rows that have scores, and records for scores that don't. When using a natural join (
JOIN
orFROM table, table2, table3
) records are only returned for which there are matching left and right values.So, your query should look like this:
Notice, you don't need to even include the
users
table here, as you're not accessing any information for it. Your query was joining the users table and then doing theWHERE
clause on it'sid
key. This is not required, unless you're getting data out of the users table. If you do, in fact, need data out of the user's table, you'd LEFT JOIN it onto the exam_scores table like so:Hope that helps and sheds some light as to how you should structure your queries and JOINs better in the future.
edit: I edited my answer in response to your comment. It makes complete sense why you wouldn't be getting responses without a grade attached, and here's why:
When you join the 2 tables together, they're being joined by the user id. That's fine, because when the user id doesn't map over to the chapters table, you get a
NULL
value for the user id and grade. However, since in ourWHERE
clause we are filtering for the user with id15
, it filters out all theNULL
values for the user columns. So, to fix against this, you add anOR
statement to yourWHERE
clause that says a user id ofNULL
is okay.