今天学长给讲了讲数据库select语句,受益匪浅,为了防止时间侵蚀,记录一下
select语句格式:
select .....
from .....
where .....
group by .....
having .....
order by..
(1)其中where中不可以使用别名,只可以使用字段名。
比如:
select id as uid,name as uname
form user_table
where uid=1 是错误的,此处只能使用字段名id而不能使用别名uid
(2)count(*)和count(字段名)的区别
当我们想查找一个表中符合条件记录的个数时会使用count()函数,count(*)和count(字段名)是有区别的,后者在字段名为NULL时会忽略此记录,而前者不会。也就是说当字段名不为NULL时,二者才没有区别。另外count(*)并非取出所有字段后再统计,不会影响到性能问题。
(3)连接的四种方式,内链接,左外连接,右外连接和全外连接
内链接:
两种写法,一种是 select from table1,table2,table3 where table1.id=table2.id and table2.name=table3.name 第二种是:select from table1 inner join table2 on table1.id=table2.id inner join table3 on table2.name=table3.name
左外连接: 表A: 表B: id id 3 3 8 8 19 24 左外连接后应为: id id 3 3 8 8 19 NULL 语句为:select from A left join B on A.id=B.id
右外连接: 关键字 right join on 和左外连接正好相反
全外连接: id id 3 3 8 8 19 NULL NULL 24 语句:select from A full outer join B on A.id=B.id