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

数据库sql语句(count(*)和count(字段))

例题:

创建如下两张表 分别命名为books和persons

 

(1)按照书名,姓名的顺序列出字里包含‘德’字的人物的姓名,书名和字。

select name 姓名,bookname 书名,style 字
from books,persons
where style like '%德%'
and books.bookid = persons.bookid
order by 2,1;

order by 2,1:先按照书名排序,书名相同再按照姓名排序:一般是按照首字母排序

 

(2)哪本书中的人物姓名包含最多‘悟’字?
第一种表示方法:

select bookname 书名
from books,persons
where persons.bookid=books.bookid
and name like '%悟%'
group by bookname
having count(books.bookid)=(select top 1 count(bookid)
from persons
where name like '%悟%'
order by count(bookid) desc)

第二种表示方法:

select bookname 书名 from books
where bookid in(
select bookid from persons
where name like'%悟%’
group by bookid
having count(*) =(
select top (1) count(*) from persons
where name like'%悟%’
group by bookid
order by 1 desc))

count(字段)和count(*)的区别:

count(*) 是统计行数,

count(字段) 是统计字段列非null的行数

在查询字段非null时,两者的查询结果是一样的

补充:

count(1)也是统计行数

对于count(1)和count(*)返回的结果是一样的,但是两者的效率在不同的情况下不同:

如果表中没有主键 ,使用count(1)比count(*)快;

如果有主键,那么count(主键)最快

详细的内容,推荐这篇:http://t.csdn.cn/AiVUl

(3)人物最多的书的男性人物的姓名和字是什么

select name 姓名,style 字
from persons
where sex='男'
and bookid in(select bookid from persons
group by bookid 
having count(bookid) =(select top 1 count(bookid) from persons
group by bookid
order by count(bookid) desc))

 

(4)哪本书的人物姓名都是三个字的

select bookname 书名
from books
where bookid not in(select bookid from persons
where name not in(select name from persons where len(name)=3))

(5)女性人物最多的书是哪个朝代的

select dynasty 朝代
from books
where bookid in (
select bookid from persons
where sex ='女’
group by bookid
having count(*)=(
select top(1) count(*) from persons
where sex='女
group by bookid
order by 1 desc

 

 

(6)与唐僧在同一本书的女性人物是谁?

select name 姓名 from books,persons
where books.bookid in(select books bookid from books,persons
where books.bookid=persons.bookid
and name ='唐僧')
and books.bookid=persons.bookid
and sex='女';

(7) 有女性人物但是女性人物最少的书:

select distinct bookname书名,author作者
from books.persons
where books.bookid=persons.bookid
and books.bookid in 
select bookid from persons
where sex ='女'
group by bookid
having count(*) =(
select top(1) count(*)
from persons
where sex='女'
group by bookid
order by 1 asc))

 

http://www.lryc.cn/news/67628.html

相关文章:

  • 短视频矩阵源码系统
  • 检测数据类型
  • 【2023春招】4399 web后台-Java后端开发
  • 干货分享:PCB防静电设计的必要性
  • 电脑压缩包文件不见了怎么办?2种办法轻松找回电脑丢失文件!
  • 如何申请gpt4.0-如何接入ChatGPT4
  • 设计模式-备忘录模式
  • 阿里、京东等大厂年薪50w的测试都是什么水平?
  • Java PECS(Producer Extends Consumer Super)原则
  • Learn RabbitMQ with SpringBoot
  • 定时器 POSIX Timer定时器和setitimer定时器
  • DeSD:用于3D医学图像分割的深度自蒸馏自监督学习
  • MySQL数据库——MySQL创建触发器(CREATE TRIGGER)
  • Java实现网上人才招聘系统【附源码】
  • jmeter接口测试项目实战详解,零基础也能学,源码框架都给你
  • MySQL中去重 distinct 和 group by 是如何去重的
  • 在职读研是理想还是情怀?你想要的都将在社科大能源管理硕士项目实现
  • 携手共建数字钢铁,Hightopo亮相第三届钢铁展洽会
  • Leetcode2383. 赢得比赛需要的最少训练时长
  • js代码执行过程、调用栈、执行上下文
  • 互联网摸鱼日报(2023-05-12)
  • 【Python从入门到实践3.1】扑克发牌知识点(range函数,def函数,else语句配合使用,random库,列表推导式)
  • Spring Cloud第二季--Spring Cloud Bus
  • Unittest自动化测试之unittestunittest_生成测试报告
  • 一个查询IP地理信息和CDN提供商的离线终端工具
  • RflySim平台使用篇 | Rflysim3D软件使用系列教程(二)
  • 2023 年第五届河南省 CCPC 大学生程序设计竞赛
  • nginx liunx最新版本安装flask部署
  • 热图 -- pheatmap or ggplot2
  • EIScopus检索 | 2023年智能交通与未来出行国际会议(CSTFM 2023)