本文介绍了加入MySQL以获取多个表数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个表用户并关注.我想编写视图,以便它将获取特定用户的所有详细信息以及这两个额外的列,作为关注者计数和关注者计数别名.
I have two table user and follow. I want to write view such that it will fetch all details of particular user along with that two extra column as follower count and followee count alias.
例如user id = 11,然后是用户表中的所有详细信息,再加上followcount 1和count1
eg. user id=11 then all details from user tables plus followcount 1 and followed count1
推荐答案
SELECT u.id,
u.userid,
u.name,
u.mobile,
(SELECT Count(*)
FROM follow f
WHERE f.followerid = u.userid) AS follower,
(SELECT Count(*)
FROM follow f
WHERE f.followeeid = u.userid) AS followee
FROM users u
这篇关于加入MySQL以获取多个表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!