从多个表中选择值 - SQL

时间:2015-05-13 06:51:29

标签: mysql sql

您好我有以下数据库结构:

users包含:email, name

connections包含:followingemail, followedemail, isfollowedaccept

locations包含:location, email

我希望在以下情况下获得我朋友的namelocation:我的电子邮件的价值是followingemail而且我的价值是我朋友的电子邮件是followedemail

只有当connctions表格中有一行包含我的电子邮件为followingemail且我朋友的电子邮件为followedemailisfollowedaccept为真时,它才会检索数据

我的问题是如何编写该查询?

我无法理解。感谢。

2 个答案:

答案 0 :(得分:0)

试试这个:

select u.name, l.location
from connections C
join users U on U.followedemail =  C.email
join location l on l.email =  C.email 
where C.followingemail = (your email)
and   U.followedemail = (your friend's email)
and U.isfollowedaccept = true

答案 1 :(得分:0)

您需要加入locationsselect u1.name as following_name, u2.name as followed_name, l1.location as following_location, l2.location as followed_location from connections c join users u1 on u1.email = c.followingemail join users u2 on u2.email = c.followedemail join locations l1 on l1.email = c.followingemail join locations l2 on l2.email = c.followedemail where c.followingemail = '{your email}' and c.followedemail = '{friends email}' and c. isfollowedaccept = 'true' 两次以获取数据,例如

getListView().smoothScrollToPositionFromTop(position,offset,duration);