【发布时间】:2010-11-21 19:25:20
【问题描述】:
我正在使用三个 mysql 表,如下所示:
会员账户表格
| id | name | status |
-------------------------------------------
| 1 | mike | 0 |
| 2 | peter | 1 |
| 3 | john | 1 |
| 4 | any | 1 |
好友列表:
| myid | user | date |
------------------------------------------
| 10 | 2 | 2010-01-04 |
| 3 | 10 | 2010-09-05 |
| 4 | 10 | 2010-10-23 |
用户画廊表:
| fotoid | userid | pic1 |
------------------------------------------
| 101 | 2 | 1.jpg |
| 102 | 3 | 2.jpg |
| 103 | 4 | 3.jpg |
我想加入这三个表并得到查询结果,它将列出我添加到朋友列表中的用户和同时添加到列表中的用户,所有这些用户的状态必须为'1 '从成员表中显示他们从图库表中的照片。
在本例中,我的 ID 是“10”。在画廊字段的表格中,“MyID”代表将其他用户添加到他们的朋友中的用户,而“用户”是添加用户的 ID。
此示例中的最终结果应如下所示:
| id | name | status | pic1 |
------------------------------------------------
| 2 | peter | 1 | 1.jpg |
| 3 | john | 1 | 2.jpg |
| 4 | any | 1 | 3.jpg |
我该怎么做?
mysql 解释:
id | select_type | table | type | possible_keys | key | key_len | ref | rows | extra
-------------------------------------------------------------------------------------------------------------------------------------------------
1 | primary | g | ALL | id | NULL | NULL | NULL | 7925 | Using where
1 | primary | a | eg_ref | id | id | 4 | g.id | 1 | Using where
2 |DEPENDENT SUBQUERY| a2 | index | id | NULL | NULL | NULL | 90734 | Using index; Using temporary; Using filesort;
2 |DEPENDENT SUBQUERY| f | index | rds_index |rds_index| 8 | NULL | 138945 | Using where;Using index;Using join buffer
【问题讨论】:
-
如果你不能加入为什么不学习你有你需要的dev.mysql.com/doc/refman/5.1/en/join.html