MySQL basics --- multi-table query

One. After grouping results filtering
Select the field name to be displayed 1, the field name to be displayed 2, the function from table name group by the field name to be displayed 1, the field name to be displayed 2 Having conditions;

例如:/* 地区的男女人数>1的,且性别不为其他的*/
SELECT guestArea,guestSex,COUNT(*) FROM  guestinfo 
GROUP BY guestArea,guestSex 
HAVING COUNT(*)>1 AND guestSex NOT LIKE '%其他%'

two. Summary (grouped results)

Select 要显示的字段名1 ,函数() from 表名 group by 要显示的字段名1
With rollup.
例如:
查询出每个地区的人数,并且汇总
SELECT guestArea,COUNT(*) FROM guestinfo GROUP BY guestArea WITH ROLLUP

three. Connection query
1. Interconnect

Select * from 表名1  inner join 表名2 on 表名1.字段名=表名2.字段名(注意:字段名相同)
例如:查询嘉宾表和商品表里的数据,并且要求显示的guestid相同且不为空
SELECT * FROM guestinfo INNER JOIN productinfo 
ON guestinfo.guestId=productinfo.guestid

2. Query the corresponding content:

SELECT guestinfo.guestName 姓名,guestinfo.guestsex 性别, 
productinfo.productname 产品名,productinfo.productprice 价格
FROM guestinfo 
INNER JOIN productinfo 
ON guestinfo.guestId=productinfo.guestid

3. Multi-table query

Select * from 表名1 ,表名2  where 表名1.字段名=表名2.字段名(注意:字段名相同)

4. External connection

1)左外连接
Select * from 表名1  left join 表名2 on 表名1.字段名=表名2.字段名(注意:字段名相同)

(2)右外连接
Select * from 表名1  right  join 表名2 on 表名1.字段名=表名2.字段名(注意:字段名相同)

Published 32 original articles · Likes 96 · Visits 1583

Guess you like

Origin blog.csdn.net/qq_44534541/article/details/105501096