A-A+

SQL基本语法(四)多表查询

2016年01月12日 sql 暂无评论 阅读 2,623 views 次

多表查询

 

select albums.name,albums.year,artists.name from albums,artists;

 

SELECT * FROM albums JOIN artists ON albums.artist_id = artists.id;

 

SELECT * FROM albums LEFT JOIN artists ON albums.artist_id=artists.id;

 

select albums.name as 'Album',albums.year,artists.name as 'Artist' from albums join artists on albums.artist_id = artists.id where albums.year > 1980;

 

总结:

Primary Key is a column that serves a unique identifier for row in the table. Values in this column must be unique and cannot be NULL.

Foreign Key is a column that contains the primary key to another table in the database. It is used to identify a particular row in the referenced table.

Joins are used in SQL to combine data from multiple tables.

INNER JOIN will combine rows from different tables if the join condition is true.

LEFT OUTER JOIN will return every row in the left table, and if the join condition is not met, NULL values are used to fill in the columns from the right table.

AS is a keyword in SQL that allows you to rename a column or table in the result set using an alias.

标签:

给我留言