基本查询

Posted on 2021-03-03 15:25  夜雨初凉  阅读(36)  评论(0)    收藏  举报

基本查询

查询:

查询所有列:

select * from 表名 where 条件

示例:

--查询所有金牌信息
select * from king
--查询所以中国金牌信息
select * from king where country = '中国'

注:“*”表示所有的列

查询指定列:

select 列名1as 别名,列名2 别名2 from 表名 where 条件

示例:

--查询所有金牌获得者的姓名
select athlete from king
--查询中国军团金牌获得者的信息的姓名
select athlete from king where country = '中国'
--查询中国军团金牌获得者的姓名与小项目名
select athlete,smallitem from king where country = '中国'

NULL值查询:

查询条件 对应代码
is null
不为空 is not null

示例:

--查询国家为NULL的运动员的金牌信息
select * from king where country is null

--查询国家不为NULL的运动员的金牌信息
select * from king where country is not null

连接字符串:

并列查询多个列
示例:

--查询NBAStar表中的所有球星的名字,将他们的姓和名连接起来
--比如勒布朗.詹姆斯
select firstname+'.'+lastname '姓名' from NBAStar

排序:

order by 列名1 asc(升序)/desc(降序),列名2 asc(默认)/desc

示例:

--查询所有金牌获得者的信息并按时间进行升序排序
select * from king order by getkingdate asc

--查询中国军团金牌获得者的信息并按时间进行降序排序
select * from king where country = '中国' order by getkingdate desc

TOP查询:

select  top n * from 表名(查询前n条数据)
select  top n percent * from 表名(查询前n%条数据)
代码单词 意思
top 顶部,前
top 3 前3条
percent 百分比
top 30 percent 前30%

示例:

--查询中国军团获得前三枚金牌
select top 3 * from king  
where country = '中国' 
order by getkingdate asc

--查询中国军团获得后三枚金牌
select top 3 * from king 
where country = '中国'
order by getkingdate desc

--查询中国军团获得前50%金牌
select top 50 percent * from king
where country = '中国'
order by getkingdate  asc

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3