关联表的查询?

WBOY
Release: 2016-06-23 13:24:41
Original
1109 people have browsed it

例如 
a 表     (分类)
label_id    label_name  
1                 羽毛球
2                 篮球
3                 足球
4                 兵乓球

b表
id    label_id    uid
1     1                1
2      2                1
3      3                1
4      2                2
5       4              2
6       1              3
7         4             3
 
c表
uid       name     sex
1          张三       男
2          李四       女
3          王五      男
4          赵六      女

现在的要求,用户点击a表某个值 显示b表中有关的c表的记录,并显示b表的值。
例如, 用户点击a 表 中的 羽毛球( label_id为1),     根据b表得知有uid为1和3喜欢羽毛球( label_id为1的值有1和3) ,那么就显示c表中的(uid为1和3) 张三和 王五的所有属性,并将张三和王五喜欢的所有显示出来(根据B表)。要求显示结果如下:

1、张三      男      羽毛球     篮球         足球
2、王五      男      羽毛球    兵乓球



我用的是thinkphp。  该如何弄?








回复讨论(解决方案)

可以这样理解,我想知道喜欢羽毛球的有哪几个,  显示出喜欢的羽毛球的所有人后,每个人并将他们喜欢的所有运动都显示出来。


$sql="SELECT c.*,GROUP_CONCAT(a.label_name) AS sport FROM a INNER JOIN b ON a.label_id=b.label_id INNER JOIN c ON b.uid=c.uid WHERE b.uid IN(SELECT b.uid FROM a INNER JOIN b ON a.label_id=b.label_id WHERE a.label_id = 1) GROUP BY uid";M()->query($sql);
Copy after login

好牛啊     牛人!!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!