源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

最全的mysql查询语句整理

  • 时间:2020-11-13 16:18 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:最全的mysql查询语句整理
[b]-- 基本查询[/b] select * from pet -- 列出指定的列 select name, owner form pet -- 直接进行算术运算,对字段起别名 select sin(1+2) as sin [b]--where 条件[/b] select * from pet where (birth>'1980' and species='dog') or species='bird' [b]-- 对null 的条件[/b] select * from pet where sex is not null [b]-- 所有名字第四位是n 的宠物信息是[/b] select * from pet where owner like '___n%' [b]-- 所有主人名叫gwen 或benny 的宠物[/b] select * from pet where owner in ('gwen' , 'benny') [b]-- 查询出生日期在90 年代是宠物,相当与 >= and   <=[/b] select * from pet where birth between '1990' and '1999' [b]-- 按主人姓名排序,相同的按宠物姓名倒序排列[/b] select * from pet order by owner, name desc [b]-- 查询性别为公的宠物,按生日倒序排列[/b] select * from pet where sex='m' order by birth desc [b]--char_lenngth() 返回的字符的长度,length() 返回字节长度[/b] SELECT owner,length(owner),char_length(owner) FROM pet p; [b]-- 列出养有宠物狗的人名[/b] select distinct owner from pet where species='dog' [b]-- 用两种方法查询出所有狗和猫的名字、出生年份、出生月份[/b] 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') [b]-- 查询所有名字中存在字母'e' 的人,将他们养的宠物按类别、年龄排序[/b] select name, species, birth from pet where owner like '%e%' order by species,birth desc [b]-- 数字函数[/b] select round(2.345,2), truncate(2.345,2), mod(323,5) [b]-- 日期函数[/b] select now(), curdate(), curtime() select adddate('2007-02-02', interval 31 day) [b]-- 求出所有宠物的年龄[/b] select name,birth, truncate(datediff(now(),birth)/365,0) as age1, year(now())-year(birth) - (dayofyear(birth)>dayofyear(now())) as age2 from pet [b]-- 分组函数[/b] select min(birth),max(birth),avg(birth),count(*),count(sex), sum(birth) from pet [b]-- 每种宠物各有几只[/b] select species,count(*) from pet group by species [b]-- 查询年龄最大的宠物的信息[/b] select * from pet where birth =    (select max(birth) from pet) [b]-- 每年各出生了几只宠物[/b] select year(birth), count(*) from pet group by year(birth) [b]-- 鸟和猫的性别比例[/b] select species, sex, count(*) from pet where species in ('cat','bird') group by species, sex [b]-- 各种宠物年龄的和[/b] select species, sum(truncate(datediff(now(),birth)/365,0)) as SumAge from pet group by species [b]-- 数量大于1 的宠物种类[/b] select species, count(*) as c from pet group by species having c>=2 [b]-- 基本双表关联[/b] select a.name,a.species, a.sex,b.date, b.type, b.remark from pet a,event b where a.name = b.name [b]-- 查询宠物产仔时的年龄[/b] 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' [b]--90 年代出生的狗的事件列表[/b] 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' [b]-- 活着的宠物按发生的事件类型分组,看各种事件发生的次数[/b] select type, count(*) from pet a, event b where a.name=b.name and a.death is null group by type [b]-- 记录的事件数量超过1 条的宠物信息[/b] select a.name,species,sex,count(*) from pet a, event b where a.name = b.name group by b.name having count(*)>=2 [b]-- 列出发生了两件事情的宠物的事件记录信息[/b] 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    ) [b]-- 插入语句[/b] 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' [b]-- 更新语句[/b] update pet set species='snake',sex='f',birth=now() where name='k' [b]-- 将事件表中生日的日期,更新到pet 表中相应宠物的birth 字段[/b] 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'             ) [b]-- 删除语句[/b] delete from pet where name like 'k%' [b]基本查询语句[/b] SELECT * FROM `test` WHERE 1                  //简单查询 SELECT id,uid FROM newdb.`test` WHERE 1            //查询ID、UID等字段 SELECT remark as r FROM `test` WHERE 1             //别名查询 SELECT * FROM `test` WHERE id=1,3               //条件查询,相等 SELECT * FROM `test` WHERE id<>2,3               //条件按查,不相等 SELECT * FROM `test` WHERE id in (1,2,4)             //in查询,即查询ID为1,2,4的数据 SELECT * FROM `test` WHERE not in (2,3)              //in查询,查询ID不是2,3的数据 SELECT * FROM `test` WHERE `uid` like '%王%'         //like模糊查询,%*%前后匹配 SELECT * FROM `test` WHERE id BETWEEN 1 and 3        //条件查询,中间数据 SELECT * FROM `test` WHERE id NOT BETWEEN 1and3      //条件查询 SELECT * FROM `test` WHERE id=1 and `remark`='学生'        //多个条件 SELECT * FROM `test` group by `remark`                      //查询排序 SELECT * FROM `test` order by `regdate` ASC                         //order by升序排序,放到limit之前 SELECT * FROM `test` order by `regdate` ASC,id DESC            //order by按照注册时间升序,ID降序 ASC 升序、DESC降序。 SELECT * FROM `test` limit 0,3                                               //数据条数限制,输出三条 SELECT count(*) FROM `test` WHERE 1                                  //统计查询,可以查询单个统计,例如count(name) SELECT max(id) FROM `test` WHERE 1                                   //统计ID最大值是多少 以下三个和以上max用法类似 MIN(*)最小值函数 AVG(*)平均值函数 SUM(*)累计值函数 [b]基本插入语句:[/b] 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()','工人')                         //简便写法,但不提倡 [b]更新语句:[/b] update test set uid='php200' where id=6                             //set 后是要改后的内容。where 后是更改位置 [b]删除语句:[/b] Delete from dbname.`test` where id=3
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部