`

最全的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%'


--
所有主人名叫gwenbenny 的宠物
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%'

 

分享到:
评论
1 楼 wangfengjiao 2010-11-15  
学习借鉴一下

相关推荐

    数据库查询指令:mysql查询语句汇总+编程知识+技术开发

    mysql查询语句汇总,数据库查询指令:mysql查询语句汇总+编程知识+技术开发;mysql查询语句汇总,数据库查询指令:mysql查询语句汇总+编程知识+技术开发;mysql查询语句汇总,数据库查询指令:mysql查询语句汇总+...

    最全sql查询语句练习题汇总(面试必备)

    最全sql查询语句练习题汇总(面试必备) 最全sql查询语句练习题汇总(面试必备) 最全sql查询语句练习题汇总(面试必备) 最全sql查询语句练习题汇总(面试必备) 最全sql查询语句练习题汇总(面试必备) 最全sql查询语句练习...

    MySQL查询语句汇总+编程知识+开发技术

    MySQL查询语句汇总+编程知识+开发技术; MySQL查询语句汇总+编程知识+开发技术; MySQL查询语句汇总+编程知识+开发技术; MySQL查询语句汇总+编程知识+开发技术; MySQL查询语句汇总+编程知识+开发技术; MySQL查询...

    mysql insert语句mysql insert语句mysql insert语句mysql insert语句mysql in

    mysql insert语句mysql insert语句mysql insert语句mysql insert语句mysql insert语句mysql insert语句mysql insert语句mysql insert语句mysql insert语句mysql insert语句mysql insert语句mysql insert语句mysql ...

    mysql常用性能查询语句

    MySQL 常用性能查询语句 MySQL 是一种关系型数据库管理系统,提供了多种方式来查询和优化数据库性能。本文总结了一些常用的 MySQL 性能查询语句,帮助数据库管理员和开发人员更好地了解和优化数据库性能。 1. 查看...

    mysql查询语句汇总.md

    mysql查询语句汇总 mysql查询语句汇总 mysql查询语句汇总 mysql查询语句汇总 mysql查询语句汇总 mysql查询语句汇总 mysql查询语句汇总

    Mysql查询重复语句

    ### MySQL 查询重复语句详解 在数据库管理与维护过程中,数据的一致性和准确性至关重要。当涉及到数据去重时,MySQL 提供了多种方法来帮助我们有效地处理数据中的重复记录。本文将详细介绍如何利用 MySQL 查询语句...

    Oracle Sql语句转换成Mysql Sql语句

    本项目提供了一个Java源码工具,能够帮助用户便捷地将Oracle SQL语句转换为MySQL SQL语句。 Oracle SQL与MySQL SQL的主要差异在于以下几个方面: 1. **数据类型**:Oracle支持的数据类型如NUMBER、LONG、RAW等在...

    MySQL查询语句写法示例

    数据库MySQL的常用查询语句和一些基本的查询语句,希望对你有帮助

    MySQL查询语句大全

    ### MySQL查询语句大全知识点详解 #### 一、基本数据库操作 **1. 查找当前服务器上的所有数据库** - **SQL语句**: `SHOW DATABASES;` - **用途**: 列出MySQL服务器上存在的所有数据库。 **2. 创建数据库** - **...

    MySQL查询语句汇总.zip

    MySQL查询语句是数据库管理中不可或缺的部分,它用于从MySQL数据库中检索数据。这份"MySQL查询语句汇总.zip"文件提供了全面的学习资料,包括相关的文档说明,非常适合对MySQL查询语句进行深入学习。 首先,我们要...

    设计高效合理的MySQL查询语句讲解

    设计高效合理的MySQL查询语句是提升数据库应用性能的关键。在数据库操作中,查询操作占据了极大的比例,特别是对于大型数据集,如银行账户信息等,优化查询语句可以显著减少查询时间,从数十分钟甚至数小时缩短至几...

    mysql查询语句练习

    查询每个雇员的编号、姓名、职位。 02.查询出所有的职位,使用DISTINCT消除掉显示的重复行记录。 03.计算每个雇员的编号、姓名、基本年薪。年薪=(工资+奖金)*12,(使用IFNULL函数判断NULL)... ...)

    oracle到mysql建表语句迁移

    oracle向mysql建表语句的迁移。 直接表结构的生成sql脚本

    mysql所有的查询语句详解

    MySQL是世界上最受欢迎的关系型数据库管理系统之一,尤其在Web开发中,与Java服务器页面(JSP)结合使用时,它的查询语句是数据交互的核心。在本篇中,我们将深入探讨MySQL在JSP中的所有主要查询语句,以及如何在...

    Excel生成MYSQL建表语句

    标题中的“Excel生成MYSQL建表语句”是指利用JAVA编程技术,通过读取预先设计好的Excel模板,自动生成对应的MySQL数据库建表语句的过程。这个过程通常涉及到数据处理、文件读写以及数据库操作等核心技能。 首先,让...

    MySQL 查询语句的汇总

    MySQL 的查询语句非常丰富和灵活,以下是一些常见的 MySQL 查询语句的汇总

    设计高效合理的MySQL查询语句

    在设计高效合理的MySQL查询语句时,关注查询性能至关重要,因为查询操作在数据库操作中占据了主要部分,而SELECT语句的执行成本最高。随着数据量的增加,全表扫描会导致查询时间显著增长,可能需要数十分钟甚至数...

    mysql查询语句汇总.zip

    这份"mysql查询语句汇总.zip"包含了丰富的学习资源,包括"mysql查询语句汇总.pdf"和"项目说明.pdf"两个文件,旨在帮助用户深入理解和掌握MySQL查询的各个方面。 首先,"mysql查询语句汇总.pdf"很可能是一个详细的...

Global site tag (gtag.js) - Google Analytics