最全的mysql查询语句整理
作者:mdxy-dxy 发布时间:2024-01-27 03:10:01
-- 基本查询
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


猜你喜欢
- 直接给源代码了:$current_dir = 'E:/temp/';$dir = opendir($current_dir)
- 很多序列的方法字符串同样适用,但是,字符串是不可变的,所以一些试图改变字符串的方法是不可用的1 字符串格式化1)用元组或者字典格式化字符串f
- 如何用ASP输出HTML文件?<!--#include file="top.inc"--><
- iterator循环器(iterator)是对象的容器,包含有多个对象。通过调用循环器的next()方法 (next()方法,在Python
- 本文实例为大家分享了js实现幸运抽奖九宫格大转盘效果,供大家参考,具体内容如下实现代码:<!DOCTYPE html><h
- 有两种类型的回调函数:blocking callbacks (also known as synchronous callbacks or
- 惊云JS随机排序程序随机显示信息-每次新闻显示顺序都不一样<script type=text/javascript>//////
- 前言:要说小时候称霸所有翻盖手机的小游戏,除了贪吃蛇,那就是推箱子了。控制小人将所有箱子放到指定位置,就是这样简简单单的操作,陪伴我度过了无
- 今天有人问我一个问题:如何通过js脚本复制网页上的一个表格?看起来似乎比较有难度,不过还是搞定了,顺便把解决这个问题的过程记录下来,希望能对
- 昨天群里介绍了一个专门帮你PS图片的网站。吐司网。网站在图片的预览处理上有点意思。当鼠标经过图片,显示为处理过的图片。这样大家能很清晰的对比
- 安装QT时在VS2019扩展里面下载QT工具时下载就一直卡在开始,如图: 网上搜索的一些改host和关ipv6的方法试了之后也没有
- 本文实例讲述了Python3实现爬取简书首页文章标题和文章链接的方法。分享给大家供大家参考,具体如下:from urllib import
- while循环:while expression: suite_to_repeatwhile 条件: 语
- 翻译自StackOverflow中一个关于Python异常处理的问答。问题:为什么“except:pass”是一个不好的编程习惯?我时常在S
- 有的小伙伴对于枚举的理解很模糊,其实我们可以把它看成一个数量的大管家,对其中的每一个数进行检查,保证里面的数字都没有重复的,这就是枚举的用法
- 虽然说IE6除了部分要求苛刻的需求以外已经被可以不考虑了,但是WIN7自带的浏览器IE8还是需要支持的。本文这个方法主要的优点,个人觉得就是
- 1. 关于箱线图 及 plt.boxplot()方法箱线图又称箱形图,有的地方也可以叫盒须图。使用箱线图的好处是可以以一种相对稳定的方式描述
- system默认:managersys默认:change_on_install使用SQL Plus登录数据库时,system使用密码mana
- 有两张表a表id val 1 a 2 b 3 c 4 d 5 e b表 a_id val 1 null 2 null 3 null 4 nu
- 示例代码如下:#!/usr/bin/python#-*- coding: utf-8 -*-import matplotlib.pyplot