概念:根据连接条件从多个表中查询选择数据,显示这些表中与连接条件相匹配的数据行,组合成新的记录.
语法:select [column_list]
FROM t1
INNER JOIN t2 ON [连接条件1]
INNER JOIN t3 ON [连接条件2]
...
WHERE where_conditions;
案例:
需求:由于图书借阅统计的需要,想查询未归还图书的图书编号,图书名称,身份证号,姓名,电话,归还日期,是否归还
分析:上述需求需要用到3张表:图书信息表,读者信息表,借阅信息表
SQL语句:
select borrowinfo.book_id, book_name, borrowinfo.card_id, name, tel, return_date, status from borrowinfo
inner join bookinfo on borrowinfo.book_id = bookinfo.book_id
inner join readerinfo on borrowinfo.card_id = readerinfo.card_id
where borrowinfo.status = '否';
通过起别名方式简化代码,内连接inner可以不写
select t1.book_id,book_name,t1.card_id, name, tel, return_date, status from borrowinfo t1
join bookinfo t2 on t1.book_id = t2.book_id
join readerinfo t3 on t1.card_id = t3.card_id
where t1.status = '否';