sql - 联合操作后如何获取记录数..?

标签 sql database union

我有这样的查询:

select author1 
from books_group 
where category='cse' 
union 
select author2 
from books_group 
where category='cse' 
union 
select author3 
from books_group 
where category='cse'

上面的查询合并了来自三个选择命令的所有记录..

我的任务是计算执行上述sql命令后我们拥有的记录数...

我尝试了下面的查询,但它给出了一个错误..

"select count(*) from (select author1 from books_group where category='cse' union select author2 from books_group where category='cse' union select author3 from books_group where category='cse') "

那么union运算后如何获取记录数..???

最佳答案

你很接近,你需要为你的子选择指定一个别名:

select
    count(*)
from
    (
    select author1 from books_group where category='cse' union
    select author2 from books_group where category='cse' union
    select author3 from books_group where category='cse'
    ) a

关于sql - 联合操作后如何获取记录数..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5204999/

相关文章:

mysql - UNION MySQL 的问题

MySQL表更新

mysql - 如何查询没有起始日期和结束日期的历史价格?

asp.net - 更改数据库密码和避免网站停机的流程

MYSQL 如何从表中选择所有电子邮件但限制具有相同域的电子邮件数量

java - H2:打开连接、更新、关闭连接,现在我的所有更改都消失了吗?

sql - 使用游标+联合+枢轴查询?

mysql - 从选择中选择 *

mysql - 帮助改进此 SQL UNION 查询

Scala 中 Set union 的性能问题