如何按用户ID加入3个表并返回存在链接的记录?

时间:2015-04-27 05:12:34

标签: mysql sql

我有3张桌子:

用户

  • 用户ID
  • 用户名

约会

  • Appointid
  • useridfk
  • Appointname
  • 开始
  • 结束

plinks

  • PID
  • useridfk
  • 链接
  • projectidfk

我想要做的是通过userid加入所有表并显示plink中存在链接的所有约会,其中users username = Mike

5 个答案:

答案 0 :(得分:0)

尝试这样的事情:

select b.* from users a join Appointments b on a.userid=b.useridfk join plinks c on a.userid=c.useridfk where a.username='Mike' and c.plinks is not null

答案 1 :(得分:0)

你尝试过这样的事吗?

SELECT * 
FROM Appointments
INNER JOIN users
ON users.userid = Appointments.useridfk
INNER JOIN plinks
ON users.userid = plinks.useridfk
WHERE users.username = 'Mike'
AND plinks.link IS NOT NULL;

答案 2 :(得分:0)

猜测表之间的关系我会这样做:

//In below code, you can replace input[type=text] with classname/id of input field. 
$('input[type=text]').on('change', function() {  
    alert($('input[type=text]').val());
});

答案 3 :(得分:0)

试试这个

select a.* from Appointments a inner join Users b on b.UserId = a.UserIdFk

内部联接PLinks c on b.UserId = c.UserIdFk 其中b.UserName ='Mike'且c.Link不为空

答案 4 :(得分:0)

你可以尝试这解决了你的问题...

SELECT * FROM Appointments as a INNER JOIN users as u ON u.userid = a.useridfk INNER JOIN plinks as p ON u.userid = p.useridfk
WHERE u.username = 'Mike' AND p.link IS NOT NULL;