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

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

例题:


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

297fa4b8c00343dd9fcc41ab81e967f0.png

ccacaf2b50a241438c47e22669b99d6d.png

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


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


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


 a0b5ec7491b94de5b11297e660c9f451.png

(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))


7ef6607f21064d3f86f8367555a40a72.png


(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))


ecc66b67d4bc46898c77a28a043f777d.png

(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


43bf294b584b4bdab3ba6d9baa4c58a3.png

(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='女';


1134b07c4e104848a67a3df46532872b.png


(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))


3f1d3146c1b547f6a27f2cada96f83bd.png

相关文章
|
8天前
|
SQL 人工智能 算法
【SQL server】玩转SQL server数据库:第二章 关系数据库
【SQL server】玩转SQL server数据库:第二章 关系数据库
51 10
|
25天前
|
SQL 存储 BI
【软件设计师备考 专题 】数据库语言(SQL)
【软件设计师备考 专题 】数据库语言(SQL)
89 0
|
29天前
|
SQL 数据库
sql server中创建数据库和表的语法
sql server中创建数据库和表的语法
18 1
|
8天前
|
SQL 算法 数据库
【SQL server】玩转SQL server数据库:第三章 关系数据库标准语言SQL(二)数据查询
【SQL server】玩转SQL server数据库:第三章 关系数据库标准语言SQL(二)数据查询
68 6
|
16天前
|
存储 关系型数据库 MySQL
MySQL数据库性能大揭秘:表设计优化的高效策略(优化数据类型、增加冗余字段、拆分表以及使用非空约束)
MySQL数据库性能大揭秘:表设计优化的高效策略(优化数据类型、增加冗余字段、拆分表以及使用非空约束)
|
5天前
|
SQL 数据库
数据库SQL语言实战(二)
数据库SQL语言实战(二)
|
5天前
|
SQL 关系型数据库 数据库
【后端面经】【数据库与MySQL】SQL优化:如何发现SQL中的问题?
【4月更文挑战第12天】数据库优化涉及硬件升级、操作系统调整、服务器/引擎优化和SQL优化。SQL优化目标是减少磁盘IO和内存/CPU消耗。`EXPLAIN`命令用于检查SQL执行计划,关注`type`、`possible_keys`、`key`、`rows`和`filtered`字段。设计索引时考虑外键、频繁出现在`where`、`order by`和关联查询中的列,以及区分度高的列。大数据表改结构需谨慎,可能需要停机、低峰期变更或新建表。面试中应准备SQL优化案例,如覆盖索引、优化`order by`、`count`和索引提示。优化分页查询时避免大偏移量,可利用上一批的最大ID进行限制。
32 3
|
8天前
|
SQL 监控 数据库
数据库管理与电脑监控软件:SQL代码优化与实践
本文探讨了如何优化数据库管理和使用电脑监控软件以提升效率。通过SQL代码优化,如使用索引和调整查询语句,能有效提高数据库性能。同时,合理设计数据库结构,如数据表划分和规范化,也能增强管理效率。此外,利用Python脚本自动化收集系统性能数据,并实时提交至网站,可实现对电脑监控的实时性和有效性。这些方法能提升信息系统稳定性和可靠性,满足用户需求。
32 0
|
8天前
|
SQL 存储 数据挖掘
数据库数据恢复—RAID5上层Sql Server数据库数据恢复案例
服务器数据恢复环境: 一台安装windows server操作系统的服务器。一组由8块硬盘组建的RAID5,划分LUN供这台服务器使用。 在windows服务器内装有SqlServer数据库。存储空间LUN划分了两个逻辑分区。 服务器故障&初检: 由于未知原因,Sql Server数据库文件丢失,丢失数据涉及到3个库,表的数量有3000左右。数据库文件丢失原因还没有查清楚,也不能确定数据存储位置。 数据库文件丢失后服务器仍处于开机状态,所幸没有大量数据写入。 将raid5中所有磁盘编号后取出,经过硬件工程师检测,没有发现明显的硬件故障。以只读方式将所有磁盘进行扇区级的全盘镜像,镜像完成后将所
数据库数据恢复—RAID5上层Sql Server数据库数据恢复案例
|
16天前
|
数据库 SQL 索引
什么是数据库 SQL Execution Plan
什么是数据库 SQL Execution Plan
11 0