我有一个三表Userregistration,paytransaction和requesttransaction,其中我必须从所有表中获取具有相同用户ID和手机号码的数据,其中我已经获得了来自用户和touser的详细信息,如fromname,fromnumber,fromemail和详细信息touser是toname,tonumber但是我也无法获得用户的电子邮件ID,它会根据联系电话号码找到.Below是我的表结构。
放在userRegistration
id contactname mobile_no email
.................................................
5 varun 8817 abc@gmail.com
112 chetan 1234 def@gmail.com
paytransaction
id userid fromname fromnumber tocontactname tocontactnumber status
...........................................................................
1 5 varun 8817 chetan 1234 pay
requesttransaction
id userid fromname fromnumber tocontactname tocontactnumber status
...........................................................................
1 112 chetan 1234 varun 8817 request
2 112 chetan 1234 ankit 123 request
如果用户在用户表中注册,则需要此输出,然后获取电子邮件ID,否则为comeblank
id userid fromname fromnumber fromemail tocontactname tocontactnumber toemail status
...........................................................................
1 5 varun 8817 abc@gmail.com chetan 1234 def@gmail.com pay
3 112 chetan 1234 def@gmail.com varun 8817 abc@gmail.com request
4 112 chetan 1234 def@gmail.com ankit 123 request
这里ankit用户没有注册,所以他的toemail id为空白,我使用了以下查询,但我无法找到基于cotactnumber.e的toemailid尝试了以下查询,但我无法获得toemail id。< / p>
SELECT Userregistration.`id`,paytransaction.`id`,paytransaction.`userid`,paytransaction.`fromname`,paytransaction.`fromnumber`,Userregistration.email as fromemail,paytransaction.`contactname` as toname,paytransaction.`contactnumber` as tonumber FROM `paytransaction` INNER JOIN Userregistration ON paytransaction.`userid`=Userregistration.`id` UNION SELECT Userregistration.`id`,requesttransaction.`id`,requesttransaction.`userid`,requesttransaction.`fromname`,requesttransaction.`fromnumber`,Userregistration.email as fromemail,requesttransaction.`contactname` as toname,requesttransaction.`contactnumber` as tonumber FROM `requesttransaction` INNER JOIN Userregistration ON requesttransaction.`userid`=Userregistration.`id`
答案 0 :(得分:1)
如果我理解你想要获得什么,你应该使用两个内连接而不是一个联合
SELECT
a.`id`
,requesttransaction.`id`
,requesttransaction.`userid`
,requesttransaction.`fromname`
,requesttransaction.`fromnumber`,
,a.email as fromemail
,requesttransaction.`toname`
,requesttransaction.`tonumber`,
,b.email as toemail
,requesttransaction.`contactname` as toname2
,requesttransaction.`contactnumber` as tonumber2
FROM requesttransaction
left JOIN Userregistration as a ON requesttransaction.`fromnumber`= a.`mobile_no`
left JOIN Userregistration as b ON requesttransaction.`tonumber`= b.`mobile_no`