SQL高级查询 -内连接 (inner) join 实战

select field1 from table1 
inner join table2 on table1.field1=table2.field2;

举个例子:

表一:

SQL高级查询 -内连接 (inner) join 实战

SQL高级查询 -内连接 (inner) join 实战

 

表二:

SQL高级查询 -内连接 (inner) join 实战

SQL高级查询 -内连接 (inner) join 实战

 

/*查出所有网站的所有访问记录*/
select * from websites join access_log
on websites.id=access_log.site_id
order by access_log.count;

SQL高级查询 -内连接 (inner) join 实战

 

select websites.name,access_log.count,access_log.date
from websites join access_log 
on websites.id=access_log.site_id
order by access_log.count;

SQL高级查询 -内连接 (inner) join 实战

 

在使用 join 时,on 和 where 条件的区别如下:

  •  1、 on 条件是在生成临时表时使用的条件,它不管 on 中的条件是否为真,都会返回表中的记录。
  •  2、where 条件是在临时表生成好后,再对临时表进行过滤的条件。