示例表A:

author_idauthor_name
1Kimmy
2Abel
3Bill
4Berton

示例表B:

book_idauthor_idstart_dateend_date
912017-09-25 21:16:042017-09-25 21:16:06
103  
1122017-09-25 21:21:462017-09-25 21:21:47
121  
138  

示例表C:

order_idbook_idpriceorder_date
190.22017-09-24 21:21:46
290.62017-09-25 21:16:04
3110.12017-09-25 21:21:46

在以上表中执行AB表关联

SELECT `authors`.*, `books`.book_id FROM `authors`
LEFT JOIN `books` ON `authors`.author_id = `books`.author_id

结果

author_idauthor_namebook_id
1Kimmy9
3Bill10
2Abel11
1Kimmy12
4Berton 

结果出现了2条author_id为1的记录,因为右表中存在了两条关联author_id=1的行

右边出现N条关联左边的记录,结果就会相应出现N条关联了右表出现的记录

在以上表中执行ABC表关联

SELECT `authors`.*, `books`.book_id, `orders`.order_id, `orders`.price FROM `authors`
LEFT JOIN `books` ON `authors`.author_id = `books`.author_id
LEFT JOIN `orders` ON `books`.book_id = `orders`.book_id

结果

author_idauthor_namebook_idorder_idorder_price
1Kimmy910.2
1Kimmy920.6
2Abel1130.1
3Bill10  
1Kimmy12  
4Berton   

结果出现了3条author_id=1的记录,因为authors第一次关联了books表book_id为9和12的book关联了author_id为1的作者,而book_id为9的书本则关联了两个orders记录,所以结果集包含3条author_id为1的记录

[MySQL]多表关联查询技巧-LMLPHP

可以运用

count(),sum()

等函数通过

group by

来统计结果

SELECT `authors`.*, sum(`orders`.price) FROM `authors`
LEFT JOIN `books` ON `authors`.author_id = `books`.author_id
LEFT JOIN `orders` ON `books`.book_id = `orders`.book_id
GROUP BY `books`.book_id

结果集会基于book_id来统计每一本书的订单总额

author_idauthor_namebook_idsum(order_price)
4Berton  
1Kimmy90.80
3Bill10 
2Abel110.10
1Kimmy12 

book_id为9的订单总额为0.80,并且9的记录从多条合并为1条。

多条件join

SELECT `authors`.*, `books`.book_id, `orders`.order_id, sum(`orders`.price) FROM `authors`
LEFT JOIN `books` ON `authors`.author_id = `books`.author_id
LEFT JOIN `orders` ON `books`.book_id = `orders`.book_id AND `orders`.order_date >= `books`.start_date AND `orders`.order_date <= `books`.end_date
GROUP BY `books`.book_id

选取在一定时间区间范围内的order订单,可以看到订单order_id为1的订单不再纳入book_id为9的统计当中,因为它的时间区间不符合join条件

author_idauthor_namebook_idorder_idsum(`order`.price)
4Berton   
1Kimmy920.60
3Bill10  
2Abel1130.10
1Kimmy12  

关于where的使用,看下面示范

SELECT `authors`.*, `books`.book_id, `orders`.order_id, sum(`orders`.price) AS prices FROM `authors`
LEFT JOIN `books` ON `authors`.author_id = `books`.author_id
LEFT JOIN `orders` ON `books`.book_id = `orders`.book_id AND `orders`.order_date >= `books`.start_date AND `orders`.order_date <= `books`.end_date
WHERE prices is not NULL
GROUP BY `books`.book_id

以上语句假设选取price不为空的记录,导致了一个错误的出现

[Err] 1054 - Unknown column 'prices' in 'where clause'

因为where不能用于选取列的AS别名判断,MYSQL的处理机制是先进行选取,再进行筛选,在选取阶段就启用了where条件,因为这时并不存在prices的筛选结果后才产生的字段,所以这里会抛出错误

我们可以这样做

SELECT `authors`.*, `books`.book_id, `orders`.order_id, sum(`orders`.price) AS prices FROM `authors`
LEFT JOIN `books` ON `authors`.author_id = `books`.author_id
LEFT JOIN `orders` ON `books`.book_id = `orders`.book_id AND `orders`.order_date >= `books`.start_date AND `orders`.order_date <= `books`.end_date
WHERE `orders`.price is not NULL
GROUP BY `books`.book_id

选取阶段order表是存在price字段的,所以只有price不为空的记录才会被选取

author_idauthor_namebook_idorder_idprices
2Abel1130.10
1Kimmy920.60

运用

having

对那些无法进行 WHERE 的AS别名的字段进行一些筛选查询

SELECT `authors`.*, `books`.book_id, sum(`orders`.price)AS prices FROM `authors`
LEFT JOIN `books` ON `authors`.author_id = `books`.author_id
LEFT JOIN `orders` ON `books`.book_id = `orders`.book_id
GROUP BY `books`.book_id
HAVING prices > 0.1

这时只有sum为0.8的结果被选中

author_idauthor_namebook_idsum(order_price)
1Kimmy90.80

对于组合其他语法查询,也是没问题的

SELECT `authors`.*, `books`.book_id, sum(`orders`.price)AS prices FROM `authors`
LEFT JOIN `books` ON `authors`.author_id = `books`.author_id
LEFT JOIN `orders` ON `books`.book_id = `orders`.book_id
GROUP BY `books`.book_id
HAVING prices >= 0.1
ORDER BY prices asc
LIMIT 1,1
05-06 23:02