結構化查詢語言(structured query language)簡稱sql,是一種特殊目的的程式語言,是一種
資料庫查詢和程式語言,用於訪問資料以及查詢、更新和管理關係資料庫系統。
alter
table tblname add
column csmid counter (0,
1)
alter
table tblname drop
column csmid
select
max(fieldname)
from tblname;
select
distinct fieldname from tblname;
select
count
(distinct fieldname)
from tblname;
select fieldname,
count(*
)from tblname
group
by fieldname
order
bycount(*
)desc
;
select sno,
count(*
)from
(select sno, sclass from students
group
by sno, sclass
)group
by sno
order
bycount(*
)desc
select date_add(datefiled,
interval
8hour
)from tbl;
# 新增自增id
select
identity
(int,1
,1)as autoid,
*into tmptable from tablename
# 選出重覆記錄中值最小的自增id
select
min(autoid)
as autoid into tmptable2 from tmptable group
by keyfield1, keyfield2
# 得到最終結果
select
*from tmptable where autoid in
(select autoid from tmptable2)
/*某些版本的mysql會有乙個警告*/
insert
ignore
into tblname(a,b,c)
values(1
,2,3
);/*不會有警告*/
insert
into tblname(a,b,c)
values(1
,2,3
)onduplicate
keyupdate c=c;
2、無則插入,有則更新。
insert
into tblname(a,b,c)
values(1
,2,3
)onduplicate
keyupdate c=c+
1;
select field from tablea
where field notin(
select field from tableb
)
2、多欄位。
select
*from tablea
where
notexists
(select
*from tableb
where tablea.field1=tableb.field1 and tablea.field2=tableb.field2
)
select
*from tablename where fieldname regexp
'^[0-9]'
limit
100;
sql常用sql語句
1 查詢某個庫中所有的表名字 select name from sysobjects where xtype u and name dtproperties order by name 2 得到資料庫中所有使用者檢視 select name from sysobjects where xtype v...
常見sql語句操作
1 基本語法 create table testfmj id int identity 1,1 identity表示自增列的意思,而int identity 1,1 表示從1開始遞增,每次自增1。name varchar 30 default abc varchar 30 age int defau...
常見SQL語句優化
一 常用sql的優化 1,優化大批量insert語句 insert into test values 1,2 2,3 6,7 不要使用insert into test values 1,2 insert into test values 2,3 insert into test values 6,7...