最全的mysql查詢(xún)語(yǔ)句整理
-- 基本查詢(xún)
select * from pet
-- 列出指定的列
select name, owner form pet
-- 直接進(jìn)行算術(shù)運(yùn)算,對(duì)字段起別名
select sin(1+2) as sin
--where 條件
select * from pet where (birth>'1980' and species='dog') or species='bird'
-- 對(duì)null 的條件
select * from pet where sex is not null
-- 所有名字第四位是n 的寵物信息是
select * from pet where owner like '___n%'
-- 所有主人名叫g(shù)wen 或benny 的寵物
select * from pet where owner in ('gwen' , 'benny')
-- 查詢(xún)出生日期在90 年代是寵物,相當(dāng)與 >= and <=
select * from pet where birth between '1990' and '1999'
-- 按主人姓名排序,相同的按寵物姓名倒序排列
select * from pet order by owner, name desc
-- 查詢(xún)性別為公的寵物,按生日倒序排列
select * from pet where sex='m' order by birth desc
--char_lenngth() 返回的字符的長(zhǎng)度,length() 返回字節(jié)長(zhǎng)度
SELECT owner,length(owner),char_length(owner) FROM pet p;
-- 列出養(yǎng)有寵物狗的人名
select distinct owner from pet where species='dog'
-- 用兩種方法查詢(xún)出所有狗和貓的名字、出生年份、出生月份
select name, left(birth,4) as year, mid(birth, 6, 2) as month from pet
where species='dog' or species='cat'
select name, year(birth) as year, month(birth) as month from pet
where species in('dog','cat')
-- 查詢(xún)所有名字中存在字母'e' 的人,將他們養(yǎng)的寵物按類(lèi)別、年齡排序
select name, species, birth
from pet
where owner like '%e%'
order by species,birth desc
-- 數(shù)字函數(shù)
select round(2.345,2), truncate(2.345,2), mod(323,5)
-- 日期函數(shù)
select now(), curdate(), curtime()
select adddate('2007-02-02', interval 31 day)
-- 求出所有寵物的年齡
select name,birth,
truncate(datediff(now(),birth)/365,0) as age1,
year(now())-year(birth) - (dayofyear(birth)>dayofyear(now())) as age2
from pet
-- 分組函數(shù)
select min(birth),max(birth),avg(birth),count(*),count(sex),
sum(birth)
from pet
-- 每種寵物各有幾只
select species,count(*)
from pet
group by species
-- 查詢(xún)年齡最大的寵物的信息
select * from pet where birth =
(select max(birth) from pet)
-- 每年各出生了幾只寵物
select year(birth), count(*) from pet group by year(birth)
-- 鳥(niǎo)和貓的性別比例
select species, sex, count(*)
from pet
where species in ('cat','bird')
group by species, sex
-- 各種寵物年齡的和
select species, sum(truncate(datediff(now(),birth)/365,0)) as SumAge
from pet
group by species
-- 數(shù)量大于1 的寵物種類(lèi)
select species, count(*) as c
from pet
group by species
having c>=2
-- 基本雙表關(guān)聯(lián)
select a.name,a.species, a.sex,b.date, b.type, b.remark
from pet a,event b
where a.name = b.name
-- 查詢(xún)寵物產(chǎn)仔時(shí)的年齡
select a.name, a.species,
truncate(datediff(b.date,a.birth)/365,0) as age
from pet a,event b
where a.name = b.name and b.type='litter'
--90 年代出生的狗的事件列表
select a.name,birth,species,sex,date,type,remark
from pet a,event b
where a.name=b.name and birth between '1990' and '1999'
and species='dog'
-- 活著的寵物按發(fā)生的事件類(lèi)型分組,看各種事件發(fā)生的次數(shù)
select type, count(*)
from pet a, event b
where a.name=b.name and a.death is null
group by type
-- 記錄的事件數(shù)量超過(guò)1 條的寵物信息
select a.name,species,sex,count(*)
from pet a, event b
where a.name = b.name
group by b.name
having count(*)>=2
-- 列出發(fā)生了兩件事情的寵物的事件記錄信息
select a.name,type,date,remark,b.species,b.sex,b.owner
from event a, pet b
where a.name=b.name and
b.name in
(
select name
from event
group by name
having count(*)=2
)
-- 插入語(yǔ)句
insert into pet (name,species,birth)
values ('KKK','snake','2007-01-01');
insert into pet
values ('KK','Diane','cat','f',null,null);
insert into pet set name='k',owner='Benny'
-- 更新語(yǔ)句
update pet set species='snake',sex='f',birth=now()
where name='k'
-- 將事件表中生日的日期,更新到pet 表中相應(yīng)寵物的birth 字段
update pet a
set birth = (
select date
from event b
where a.name=b.name and b.type='birthday'
)
where a.name in (
select name
from event
where type='birthday'
)
-- 刪除語(yǔ)句
delete from pet where name like 'k%'
基本查詢(xún)語(yǔ)句
SELECT * FROM `test` WHERE 1 //簡(jiǎn)單查詢(xún)
SELECT id,uid FROM newdb.`test` WHERE 1 //查詢(xún)ID、UID等字段
SELECT remark as r FROM `test` WHERE 1 //別名查詢(xún)
SELECT * FROM `test` WHERE id=1,3 //條件查詢(xún),相等
SELECT * FROM `test` WHERE id<>2,3 //條件按查,不相等
SELECT * FROM `test` WHERE id in (1,2,4) //in查詢(xún),即查詢(xún)ID為1,2,4的數(shù)據(jù)
SELECT * FROM `test` WHERE not in (2,3) //in查詢(xún),查詢(xún)ID不是2,3的數(shù)據(jù)
SELECT * FROM `test` WHERE `uid` like '%王%' //like模糊查詢(xún),%*%前后匹配
SELECT * FROM `test` WHERE id BETWEEN 1 and 3 //條件查詢(xún),中間數(shù)據(jù)
SELECT * FROM `test` WHERE id NOT BETWEEN 1and3 //條件查詢(xún)
SELECT * FROM `test` WHERE id=1 and `remark`='學(xué)生' //多個(gè)條件
SELECT * FROM `test` group by `remark` //查詢(xún)排序
SELECT * FROM `test` order by `regdate` ASC //order by升序排序,放到limit之前
SELECT * FROM `test` order by `regdate` ASC,id DESC //order by按照注冊(cè)時(shí)間升序,ID降序
ASC 升序、DESC降序。
SELECT * FROM `test` limit 0,3 //數(shù)據(jù)條數(shù)限制,輸出三條
SELECT count(*) FROM `test` WHERE 1 //統(tǒng)計(jì)查詢(xún),可以查詢(xún)單個(gè)統(tǒng)計(jì),例如count(name)
SELECT max(id) FROM `test` WHERE 1 //統(tǒng)計(jì)ID最大值是多少
以下三個(gè)和以上max用法類(lèi)似
MIN(*)最小值函數(shù)
AVG(*)平均值函數(shù)
SUM(*)累計(jì)值函數(shù)
基本插入語(yǔ)句:
insert into test (`id`,`uid`,`regdate`,`remark`) values ('','PHP100','2008-07-26','工人') //ID自增,
insert into test (`id`,`uid`,`regdate`,`remark`) values ('','PHP100','now()','工人')
insert into test values ('','PHP200','now()','工人') //簡(jiǎn)便寫(xiě)法,但不提倡
更新語(yǔ)句:
update test set uid='php200' where id=6 //set 后是要改后的內(nèi)容。where 后是更改位置
刪除語(yǔ)句:
Delete from dbname.`test` where id=3
相關(guān)文章
MySQL修改時(shí)間添加時(shí)間自動(dòng)更新的兩種方法
這篇文章主要介紹了MySQL修改時(shí)間添加時(shí)間自動(dòng)更新的兩種方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09在idea中如何操作MySQL數(shù)據(jù)庫(kù)
這篇文章主要介紹了在idea中如何操作MySQL數(shù)據(jù)庫(kù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06MySQL修改innodb_data_file_path參數(shù)的一些注意事項(xiàng)
這篇文章主要給大家介紹了關(guān)于MySQL修改innodb_data_file_path參數(shù)的一些注意事項(xiàng),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用MySQL具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04淺談sql語(yǔ)句中GROUP BY 和 HAVING的使用方法
GROUP BY語(yǔ)句和HAVING語(yǔ)句,經(jīng)過(guò)研究和練習(xí),終于明白如何使用了,在此記錄一下同時(shí)添加了一個(gè)自己舉的小例子,通過(guò)寫(xiě)這篇文章來(lái)加深下自己學(xué)習(xí)的效果,還能和大家分享下,同時(shí)也方便以后查閱,一舉多得,下面由小編來(lái)和大家一起學(xué)習(xí)2019-05-05mysql實(shí)現(xiàn)隨機(jī)查詢(xún)經(jīng)驗(yàn)談
官方文檔中進(jìn)行說(shuō)明:Order By和RAND()連用,會(huì)多次掃描表,導(dǎo)致速度變慢,下面看下一些測(cè)試詳解2013-10-10Linux下mysql 8.0.15 安裝配置圖文教程以及修改密碼
這篇文章主要為大家詳細(xì)介紹了Linux下mysql 8.0.15安裝配置圖文教程以及修改密碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03MySQL:Unsafe statement written to the binary log using state
這篇文章主要介紹了MySQL:Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEM,需要的朋友可以參考下2016-05-05