**************************************************************************************************************************************************************************************
三表查询
buy work user
其中buy只有3条数据
*************************** 1. row ***************************
order_id: 1
...
work_id: 1
...
*************************** 2. row ***************************
order_id: 2
...
work_id: 1
...
*************************** 3. row ***************************
order_id: 3
...
work_id: 1
...
3 rows in set (0.00 sec)
A:select work.work_id,user.user_id,user.user_name,work.name,work.price,count(*) as count from work left join user on user.user_id=work.artist_id left join buy on buy.work_id=work.work_id group by work_id;
=select work.work_id,user.user_id,user.user_name,work.name,work.price,count(*) as count from work left join user on user.user_id=work.artist_id left join buy on buy.work_id=work.work_id group by work.work_id;
列出了所有work 并且 某一个work_id有几个buy的数据 count就为几
-+
| 1 | 2 | 刘域 | 我去 | 100 | 3 |
| 2 | 2 | 刘域 | 我去 | 100 | 1 |
| 3 | 2 | 刘域 | 金色花艺 | 100 | 1 |
| 4 | 2 | 刘域 | 金色花艺 | 100 | 1 |
| 8 | 2 | 刘域 | 金色花艺 | 100 | 1 |
| 9 | 2 | 刘域 | 金色花艺 | 100 | 1 |
| 10 | 2 | 刘域 | 金色花艺 | 100 | 1 |
| 11 | 2 | 刘域 | 金色花艺 | 100 | 1 |
| 12 | 2 | 刘域 | 金色花艺 | 100 | 1 |
| 13 | 2 | 刘域 | 金色花艺 | 100 | 1 |
| 14 | 2 | 刘域 | 金色花艺 | 100 | 1 |
| 15 | 2 | 刘域 | 金色花艺 | 100 | 1 |
| 16 | 2 | 刘域 | 金色花艺 | 100 | 1 |
| 17 | 2 | 刘域 | 金色花艺 | 100 | 1 |
| 18 | 2 | 刘域 | 的点点滴滴大大大 | 100 | 1 |
| 20 | 2 | 刘域 | 的点点滴滴大大大 | 100 | 1 |
| 21 | 2 | 刘域 | 的点点滴滴大大大 | 100 | 1 |
| 22 | 2 | 刘域 | 的点点滴滴大大大 | 100 | 1 |
| 23 | 2 | 刘域 | 的点点滴滴大大大 | 100 | 1 |
| 26 | 2 | 刘域 | 的点点滴滴大大大 | 100 | 1 |
| 27 | 2 | 刘域 | 的点点滴滴大大大 | 100 | 1 |
| 29 | 2 | 刘域 | 的点点滴滴大大大 | 100 | 1 |
| 30 | 2 | 刘域 | 的点点滴滴大大大 | 100 | 1 |
| 31 | 2 | 刘域 | 的点点滴滴大大大 | 100 | 1 |
| 32 | 2 | 刘域 | 的点点滴滴大大大 | 100 | 1 |
| 33 | 2 | 刘域 | 的点点滴滴大大大 | 100 | 1 |
..................
B:select work.work_id,user.user_id,user.user_name,work.name,work.price,count(*) as count from work left join user on user.user_id=work.artist_id left join buy on buy.work_id=work.work_id group by buy.work_id;
| 2 | 2 | 刘域 | 我去 | 100 | 42 |
| 1 | 2 | 刘域 | 我去 | 100 | 3 |
只有两行,分为有buy记录的和有buy记录的work_id=1
group by work.work_id;
以work的work_id为准 每个work为一行
关联的buy个数决定count
group by buy.work_id;
以每个work_id的buy和为一列 一个work_id几个buy决定count
如果没有buy 则没有
但由于left join即使没有任何buy也会有一行数据
(删除三个buy之后 无任何buy执行上述语句
| 1 | 2 | 刘域 | 我去 | 100 | 43 |
全部数据无任何buy 无任何buy的为一组 count为43
**************************************************************************************************************************************************************************************************
SELECT user.photo,user.user_id,user.user_name,count(`order_id`) as a FROM `user` left join `order` on `order`.artist_id=user.user_id WHERE `user_type` = 1 GROUP BY user_id ORDER BY a desc LIMIT 0,10 ;
每个work_id有多少个order
联合查询最要注意主表 次表之分
可是上面的B查询却是根据group字段来自于哪个表来决定
同样
work只有
mysql> select * from work;
+---------+--------+
| work_id | author |
+---------+--------+
| 2 | 1 |
| 3 | 1 |
+---------+--------+
mysql> select user_id, user_name,work.author, count(*) from user left join work on work.author=user.user_id group by work.author;
+---------+-----------+--------+----------+
| user_id | user_name | author | count(*) |
+---------+-----------+--------+----------+
| 8 | ssss | NULL | 1 |
| 1 | ss | 1 | 2 |
+---------+-----------+--------+----------+
mysql> select author,user_id, user_name, count(*) from user left join work on work.author=user.user_id group by user.user_id;
+--------+---------+-----------+----------+
| author | user_id | user_name | count(*) |
+--------+---------+-----------+----------+
| 1 | 1 | ss | 2 |
| NULL | 8 | ssss | 1 |
+--------+---------+-----------+----------+
2 rows in set (0.00 sec)
group不同字段却得到了相同的结果