mysql 数据库学习笔记

多表查询:

此方法默认内联查询:

–inner join

1
2
select pa.product_id, pp.aa, p.a from fb_product2account pa, fb_product_price pp, fb_product p
where pa.product_id = pp.product_id and pp.aa=p.a and pa.product_id=16

此方法通用内联查询、左查询、右查询、外查询

1
2
3
4
5
6
7
8
select acc.acc_id,pprice.price,pprice.country,pro.product_name,pro.product_number, coun.* from fb_product2account acc
inner join fb_product_price pprice on acc.id = pprice.product_id
inner join fb_product pro on acc.id = pro.product_id
inner join fb_v1_country coun on coun.country = pprice.country
where acc.id = 15
group by
order by
limit
1
2
3
select * from fb_product2account pa
right join fb_product_price pp on pa.product_id = pp.product_id
right join fb_product p on pp.aa=p.a

别名:

1
2
3
4
5
select acc.acc_id,pprice.price,pprice.country,pro.product_name,pro.product_number, coun.code country_code, coun.en_name country_en_name, coun.zh_name country_zh_name from fb_product2account acc
inner join fb_product_price pprice on acc.id = pprice.product_id
inner join fb_product pro on acc.id = pro.product_id
inner join fb_v1_country coun on coun.country = pprice.country
where acc.id = 15