当前位置: 首页 > news >正文

mysql语句整理_最全的mysql查询语句整理

-- 基本查询

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.name,a.species, a.sex,b.date, b.type, b.remark

from pet a,event b

where a.name = b.name

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

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'

-- 活着的宠物按发生的事件类型分组,看各种事件发生的次数

select type, count(*)

from pet a, event b

where a.name=b.name and a.death is null

group by type

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

select a.name,species,sex,count(*)

from pet a, event b

where a.name = b.name

group by b.name

having count(*)>=2

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

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

)

-- 插入语句

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'

-- 更新语句

update pet set species='snake',sex='f',birth=now()

where name='k'

-- 将事件表中生日的日期,更新到pet 表中相应宠物的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'

)

-- 删除语句

delete from pet where name like 'k%'

基本查询语句

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(*)累计值函数

基本插入语句:

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()','工人')                         //简便写法,但不提倡

更新语句:

update test set uid='php200' where id=6                             //set 后是要改后的内容。where 后是更改位置

删除语句:

Delete from dbname.`test` where id=3

相关文章:

  • mysql 同步机制_MySQL主从同步机制及同步中的问题处理
  • win8安装mysql出现2503_Win8.1安装msi出现2503错误怎么办?
  • mysql 大表增删查改_MySQL数据表的增删改查操作
  • 组态王如何连接mysql_组态王连接SQL数据库操作步骤
  • mysql blob转换字符串_最全MySQL面试60题和答案
  • 建立MySQL数据库之后_创建Mysql数据库
  • label读取摄像头 pyqt5_PythonPyQt5:摄像机和屏幕切换
  • 怎么获取前台提交数据是几兆_Web怎么获取请求数据,修改响应信息呢?
  • python 动态规划 回溯_回溯算法和动态规划的转化
  • python中xlrd官方_Python中如何用xlrd读取
  • python字符串常用操作字符串_python字符串常用操作
  • matlab 散点图 线性回归图_简单线性回归matlab实现
  • mysql select count(1)_select count(1) 和 select count(*)的区别
  • mysql source 数据库_MySQL 数据库 source 命令详解及实例
  • mysql1044错误代码_mysql出现1044错误怎么办
  • [PHP内核探索]PHP中的哈希表
  • 【面试系列】之二:关于js原型
  • 8年软件测试工程师感悟——写给还在迷茫中的朋友
  • avalon2.2的VM生成过程
  • axios请求、和返回数据拦截,统一请求报错提示_012
  • C++回声服务器_9-epoll边缘触发模式版本服务器
  • gitlab-ci配置详解(一)
  • HTTP--网络协议分层,http历史(二)
  • JavaScript新鲜事·第5期
  • leetcode98. Validate Binary Search Tree
  • RedisSerializer之JdkSerializationRedisSerializer分析
  • Spring核心 Bean的高级装配
  • Transformer-XL: Unleashing the Potential of Attention Models
  • 测试如何在敏捷团队中工作?
  • 基于Javascript, Springboot的管理系统报表查询页面代码设计
  • 容器化应用: 在阿里云搭建多节点 Openshift 集群
  • 小程序、APP Store 需要的 SSL 证书是个什么东西?
  • 一个普通的 5 年iOS开发者的自我总结,以及5年开发经历和感想!
  • 用Python写一份独特的元宵节祝福
  • 《TCP IP 详解卷1:协议》阅读笔记 - 第六章
  • C# - 为值类型重定义相等性
  • hi-nginx-1.3.4编译安装
  • Spring Batch JSON 支持
  • ​LeetCode解法汇总2583. 二叉树中的第 K 大层和
  • ​力扣解法汇总1802. 有界数组中指定下标处的最大值
  • #QT项目实战(天气预报)
  • (4) openssl rsa/pkey(查看私钥、从私钥中提取公钥、查看公钥)
  • (4)(4.6) Triducer
  • (安卓)跳转应用市场APP详情页的方式
  • (一)【Jmeter】JDK及Jmeter的安装部署及简单配置
  • (原創) 如何安裝Linux版本的Quartus II? (SOC) (Quartus II) (Linux) (RedHat) (VirtualBox)
  • (转)创业家杂志:UCWEB天使第一步
  • ***详解账号泄露:全球约1亿用户已泄露
  • .NET Core Web APi类库如何内嵌运行?
  • .Net Core/.Net6/.Net8 ,启动配置/Program.cs 配置
  • .net framework 4.0中如何 输出 form 的name属性。
  • .Net mvc总结
  • .net6使用Sejil可视化日志
  • /run/containerd/containerd.sock connect: connection refused
  • @property @synthesize @dynamic 及相关属性作用探究