mysql数据库查询

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

最全的mysql查询语句(审精)

null, 名字, 宠物

--基本查询

select * from pet

--列出指定的列

select name, owner form pet

--直接进行算术运算,对字段起别名

select sin(1+2) as sin

--where条件

select * from pet where (birth>'1980' and species='dog') or species='bird'

--对null的条件

select * from pet where sex is not null

--所有名字第四位是n的宠物信息是

select * from pet where owner like '___n%'

--所有主人名叫gwen或benny的宠物

select * from pet where owner in ('gwen' , 'benny')

--查询出生日期在90年代是宠物,相当与 >= and <=

select * from pet where birth between '1990' and '1999'

--按主人姓名排序,相同的按宠物姓名倒序排列

select * from pet order by owner, name desc

--查询性别为公的宠物,按生日倒序排列

select * from pet where sex='m' order by birth desc

--char_lenngth()返回的字符的长度,length()返回字节长度

SELECT owner,length(owner),char_length(owner) FROM pet p;

--列出养有宠物狗的人名

select distinct owner from pet where species='dog'

--用两种方法查询出所有狗和猫的名字、出生年份、出生月份

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')

--查询所有名字中存在字母'e'的人,将他们养的宠物按类别、年龄排序

select name, species, birth

from pet

where owner like '%e%'

order by species,birth desc

--数字函数

select round(2.345,2), truncate(2.345,2), mod(323,5)

--日期函数

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

--分组函数

select min(birth),max(birth),avg(birth),count(*),count(sex),

sum(birth)

from pet

--每种宠物各有几只

select species,count(*)

from pet

group by species

--查询年龄最大的宠物的信息

select * from pet where birth =

(select max(birth) from pet)

--每年各出生了几只宠物

select year(birth), count(*) from pet group by year(birth)

--鸟和猫的性别比例

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

--数量大于1的宠物种类

select species, count(*) as c

from pet

group by species

having c>=2

--基本双表关联

select ,a.species, a.sex,b.date, b.type, b.remark

from pet a,event b

where =

--查询宠物产仔时的年龄

select , a.species,

truncate(datediff(b.date,a.birth)/365,0) as age

from pet a,event b

where = and b.type='litter'

--90年代出生的狗的事件列表

select ,birth,species,sex,date,type,remark

from pet a,event b

where = and birth between '1990' and '1999' and species='dog'

--活着的宠物按发生的事件类型分组,看各种事件发生的次数select type, count(*)

from pet a, event b

where = and a.death is null

group by type

--记录的事件数量超过1条的宠物信息

select ,species,sex,count(*)

from pet a, event b

where =

group by

having count(*)>=2

--列出发生了两件事情的宠物的事件记录信息

select ,type,date,remark,b.species,b.sex,b.owner from event a, pet b

where = and

in

(

select name

from event

group by name

having count(*)=2

)

相关文档
最新文档