查詢
select * from tana
column as 別名 或 column 別名
select id as ID, name NAME from tana
只查詢某一段 limit 5,20 第一個(gè)參數(shù)是從幾行開始檢索,第二個(gè)參數(shù)是結(jié)果行數(shù)。一般分頁(yè)都是這么處理的
select * from tana limit 5,20
where 子句 <> 不等于,between 在某個(gè)范圍內(nèi), not between 不在范圍類。 AND 和 OR 可在 WHERE 子語(yǔ)句中把兩個(gè)或多個(gè)條件結(jié)合起來。
where column > < >= <= <> (not | between id = 5 and id =10) LIKE
只查詢某個(gè)范圍where age between 20 and 25 等價(jià)于 where age >=20 and age <=25
select * from tana where age between 20 and 25
多條件查詢 in 等價(jià)于 name = 'drip' or name = 'drip2' (盡量不要用in(不能用索引),數(shù)據(jù)越多效率越低)
selsct * from tana where name in ('drip','drip2')
按條件查詢 where。 and 同時(shí)滿足。 or 滿足任意一個(gè)
select * from tana where name = 'drip' and age = 20 or name = 'drip2'
基本函數(shù) avg(id)平均值。 sum(id)求和。 max(id)最大值, min(id) 最小值,count(id)總數(shù)。
select avg(id),sum(id),max(id),min(id),count(id) from tana where tana
模糊查詢 like '%d%' 查詢內(nèi)容包含有d的結(jié)果。 ‘%d’d結(jié)尾的數(shù)據(jù)。 'd%'d開頭的數(shù)據(jù)。'_d' 值為?d的結(jié)果??山Y(jié)合使用。 ‘_r%d’ 比如 drip,?r???d。 not like 表示不包含。 通配符 % _ [] [!]
select * from tana where name like '%d%' |
排序 order by id asc 或 desc (默認(rèn)asc升序) 當(dāng)多個(gè)參數(shù)時(shí),當(dāng)?shù)谝粋€(gè)參數(shù)相同時(shí),按第二個(gè)參數(shù)排序。以此類推。
select * from tana order by id asc, name desc
分組 group by sex 。 按sex分組。獲取男女總數(shù)。 一般配合聚合函數(shù)使用。
select sex,count(id) from tana group by sex
多表查詢 inner,只返回滿足條件的結(jié)果。 left以左表為基礎(chǔ),返回滿足條件的結(jié)果(左表行全部返回,左表有,右表沒有,則右表列顯示為null)。 right, (以右表為基礎(chǔ),返回滿足條件的結(jié)果(右表行全部返回,右表有,左表沒有,則左表列顯示為null)。
select * from bana1 left join bana2 on bana1.id = bana2.b1id
多個(gè)結(jié)果集組合(兩個(gè)表的列數(shù)量要相同,類型也要相同) union(會(huì)刪除數(shù)據(jù)相同的行) , union all,(結(jié)果是前10行和后10行1 2 3...10 20 19 18...11);
SELECT * FROM tana limit 10 union all (SELECT * FROM tana order by id desc limit 10)
is null 和 is not null;
獲取字段不為空的數(shù)據(jù) is null; is not null
SELECT * FROM tana where name is not null
插入
INSERT INTO 表名稱 VALUES (值1, 值2,....)
INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....)
更新
UPDATE 表名稱 SET 列名稱 = 新值 WHERE 列名稱 = 某值
update base_user set name = 'drip' where id = '1';
刪除
DELETE FROM 表名稱 WHERE 列名稱 = 值