1. 程式人生 > >SQL常用語句大全

SQL常用語句大全

price 所有 更新 rom ack 內容 表名 like between

1.插入數據

insert into 表名(列1,列2,列3)values(值1,值2,值3);

insert into product(name,price,pic_path) values(‘jack‘,25,‘updown‘);

2.更新數據

update 表名 set 列名1=值1,列名2=值2 where 條件;

update product set name="jack",price=35 where id=2;

3.刪除數據庫

delete from 表名[where 條件]

delete from product where id=2;

4.查詢所有數據庫內容

select * from 表名;

select * from product;

5.查詢部分列

select 列1,列2 from 表名;

select id,name from product;

6.條件查詢

#比較 =,>,<,>=,<=,!=

select * from 表名 where列名=值;

select * from product where id=2;

#and與

select * from 表名 where 條件1 and 條件2 and 條件3;

select * from product where id=2 and name=‘Nike‘;

#or 或

select * from 表名 where 條件1 or 條件2 or 條件3;

select * from product where name=‘Nike‘ or id=2;

#not 非

select * from 表名 where not 條件1;

select * from product where not name=‘Nike‘;

#in枚舉

select * from 表名where 列名 in(值1,值2,值3);

select * from product where id in(2,3,4,10);

select * from product where id not in(2,3,4);

#like模糊查詢

select * from 表名where列名 like ‘%值%‘;

select * from product where name like ‘%LI%‘;

#between....and 範圍查詢

select * from 表名 where 列名 between 值1 and 值2;

select * from product where created between ‘2010-10-10‘ and ‘2011-10-10‘;

#limit行數查詢

select * from 表名 limit n1,n2;(n1:從第幾行開始,從0開始算;n2:要顯示幾行)

select * from product limit 3,4;

7.查詢排序

select * from 表名 order by列表排序方式。

#排序方式:asc(升序),desc(降序);

select * from product order by created desc;

8.聚合函數

#count 總記錄數

select count(列名) from student;

select count(id) from student;

#sum 總共

select sum(列名) from student;

select sum(age) from student;

#avg 平均值

select avg(列名)from student;

select avg(age) from student;

#max 最大值

select max(列名)from student;

select max(age) from student;

#min 最小值

select min(列名) from 表名;

select min(age) from student;

9.子查詢

select name from student where age<(select avg(age) from student);

select * from product where id in(select id from order);

SQL常用語句大全