-- 基本查询
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%'
分享到:
相关推荐
对Sql语句的格式进行整理,使其易读。 操作简单,只需拷贝Sql语句,执行整理程序,然后再粘贴,就成为格式清晰的Sql语句了。
自己平时工作中整理的一点语句,都很使用,希望大家能用上。
通过本章学习,您将可以: 列举 SQL SELECT语句的功能。 执行简单的选择语句。 SQL 语言和 SQL*Plus 命令的不同
自己整理的常用SQL语句用法,附带简单的实例,易学易懂。
《SQL语句性能优化》有些程序员在撰写数据库应用程序时,常专注于 OOP 及各种 framework 的使用,却忽略了基本的 SQL 语句及其「性能 (performance) 优化」问题。曾听过台湾某半导体大厂的新进程序员,所组出来的一...
如果你的项目中使用了EF Core, 且正在处于性能调优阶段,那么了解EF Core生成的SQL语句是非常关键的。那么除了使用第三方工具,如何查看EF Core生成的SQL语句呢?这里笔者将给出一个基于.NET Core内置日志组件的实现...
一般采用带like的SQL语句进行简单的拼接字符串时,需要开率遇到sql注入的情况。这确实是个需要注意的问题。 这里结合一些查阅的资料做了初步的整理。 如这样一个sql语句: select * from game where gamename like ...
一些自己学过的PLSQL语法然后整理的笔记,以及 oracle中的sql语句比较简单容易理解
自己整理了下基本的SQL语句,重新复习了下SQL使用。都是些最基础的。DataBase创建、增删该查、存储过程、触发器、事务、游标等简单的操作
mysql的简单语句整理
1、使用SQL Server 2000企业管理器和查询分析器工具(即用Transact-SQL语句)创建一个“图书读者数据库”(Book_Reader_DB); 2、使用企业管理器查看Book_Reader_DB的数据库属性,并进行修改,使之符合你的要求; ...
Hive的优点是学习成本低,可以通过类似SQL语句实现快速MapReduce统计,使MapReduce变得更加简单,而不必开发专门的MapReduce应用程序。hive是十分适合数据仓库的统计分析和Windows注册表文件。 本文是Hive的开发...
分组后分组合计以及总计SQL语句 1)想一次性得到分组合计以及总计,sql: SELECT 分组字段 FROM 表 GROUP BY 分组字段 compute sum(COUNT(*)) 2)分组合计1: SELECT COUNT(*) FROM (SELECT 分组字段 FROM 表 ...
收录最全面的全国省市区数据,已整理成sql语句,其中各表关联请自行分析(很简单)
精心收集整理的各种SQL语句示例,帮助您更加容易的熟悉和使SQL。 1 DECLARE @local_variable 1.1 使用 DECLARE 以下示例将使用名为 @find 的局部变量检索所有姓氏以“Man”开头的联系人信息。 USE AdventureWorks;...
针对sqlserver语句的一些基础学习,大四即将结束想把大学里做的项目进行简单的归纳整理
一般情况下,正常安装sql server之后,master等系统数据库的路径都是被设置为绝对路径,如果要改正数据库的路径为相对路径,可以使用以下SQl语句: sql:=format(' update sysaltfiles set filename='%s'...
以上四个SQL在ORACLE分析整理之后产生的结果及执行的时间是一样的,但是从ORACLE共享内存SGA的原理,可以得出ORACLE对每个SQL 都会对其进行一次分析,并且占用共享内存,如果将SQL的字符串及格式写得完全相同则...
Hive的优点是学习成本低,可以通过类似SQL语句实现快速MapReduce统计,使MapReduce变得更加简单,而不必开发专门的MapReduce应用程序。hive是十分适合数据仓库的统计分析和Windows注册表文件。 该文档是我整理的...