gpt4 book ai didi

mysql - 在mysql中俱乐部两个查询

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

我编写了以下查询

 SELECT Registration.FName,RequestPooja.RitualId AS RitualId FROM RequestPooja INNER JOIN Registration
ON RequestPooja.UserId = Registration.Id WHERE Registration.Id=2
UNION ALL
SELECT Registration.FName,AaparaKarma.RitualId AS RitualId FROM AaparaKarma INNER JOIN Registration
ON AaparaKarma.UserId = Registration.Id
WHERE Registration.Id=2

其产生的结果为

FName   RitualId
Shreyas 24
Shreyas 37
Shreyas 37
Shreyas 37

现在我有一个仪式表

SELECT Rituals.Id,Rituals.Name FROM Rituals

输出为

 Id    Name
24 ABC
37 xyz

现在我想将这两个查询合并在一起,并且希望得到所需的结果

 FName  RitualId    Name
Shreyas 24 ABC
Shreyas 37 xyz
Shreyas 37 xyz
Shreyas 37 xyz

我应该使用联接还是内部查询来获得所需的结果?任何帮助表示赞赏。

最佳答案

检查这个。使用 Join 因为它可以提供更快的性能。

SELECT Registration.FName,RequestPooja.RitualId AS RitualId ,Rituals.Name 
FROM RequestPooja
INNER JOIN Registration ON RequestPooja.UserId = Registration.Id
INNER JOIN Rituals ON Rituals.Id=RequestPooja.RitualId
WHERE Registration.Id=2

UNION ALL

SELECT Registration.FName,AaparaKarma.RitualId AS RitualId ,Rituals.Name
FROM AaparaKarma
INNER JOIN Registration ON AaparaKarma.UserId = Registration.Id
INNER JOIN Rituals ON Rituals.Id=AaparaKarma.RitualId
WHERE Registration.Id=2

关于mysql - 在mysql中俱乐部两个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43360594/

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