我如何合并 mysql 中的多个连接(给出示例查询)
how can i merge multiple joins in mysql (sample query given)
我需要为多个学生重复以下加入。如何优化并将其编写为单个查询?
select * from marks mar
join subject subj ON mar.subject_id=subj.subject_id
join student std ON subj.student_id = std.sutdent_id
Where std.student_name="xyz" and std.location="abc" and age="18"
select * from marks mar
join subject subj ON mar.subject_id=subj.subject_id
join student std ON subj.student_id = std.sutdent_id
Where std.student_name="ccc" and std.location="kkk" and age="19"
是...
select * from marks mar
join subject subj ON mar.subject_id=subj.subject_id
join student std ON subj.student_id = std.sutdent_id
Where
(std.student_name="xyz" and std.location="abc" and age="18")
OR
(std.student_name="ccc" and std.location="kkk" and age="19")
...过于简单化了吗?
这将在 MySQL 中解析:
SELECT *
FROM marks mar
JOIN subject subj
ON subj.subject_id = mar.subject_id
JOIN student std
ON std.student_id = subj.student_id
WHERE (std.student_name, std.location, age) IN (('xyz', 'abc', 18), ('ccc', 'kkk', 19))
但没有给出更好的执行计划
或者,使用这个:
SELECT *
FROM marks mar
JOIN subject subj
ON subj.subject_id = mar.subject_id
JOIN student std
ON std.student_id = subj.student_id
WHERE (std.student_name, std.location, age) = ('xyz', 'abc', 18)
OR
(std.student_name, std.location, age) = ('ccc', 'kkk', 19)
我需要为多个学生重复以下加入。如何优化并将其编写为单个查询?
select * from marks mar
join subject subj ON mar.subject_id=subj.subject_id
join student std ON subj.student_id = std.sutdent_id
Where std.student_name="xyz" and std.location="abc" and age="18"
select * from marks mar
join subject subj ON mar.subject_id=subj.subject_id
join student std ON subj.student_id = std.sutdent_id
Where std.student_name="ccc" and std.location="kkk" and age="19"
是...
select * from marks mar
join subject subj ON mar.subject_id=subj.subject_id
join student std ON subj.student_id = std.sutdent_id
Where
(std.student_name="xyz" and std.location="abc" and age="18")
OR
(std.student_name="ccc" and std.location="kkk" and age="19")
...过于简单化了吗?
这将在 MySQL 中解析:
SELECT *
FROM marks mar
JOIN subject subj
ON subj.subject_id = mar.subject_id
JOIN student std
ON std.student_id = subj.student_id
WHERE (std.student_name, std.location, age) IN (('xyz', 'abc', 18), ('ccc', 'kkk', 19))
但没有给出更好的执行计划
或者,使用这个:
SELECT *
FROM marks mar
JOIN subject subj
ON subj.subject_id = mar.subject_id
JOIN student std
ON std.student_id = subj.student_id
WHERE (std.student_name, std.location, age) = ('xyz', 'abc', 18)
OR
(std.student_name, std.location, age) = ('ccc', 'kkk', 19)