这个SQL语句该怎么写?

slowgrace 2009-03-10 01:40:25
我有多个表(表1、表2、表3、表4、表5),都具有name字段和value字段和id字段,其他字段不同。现在想知道这所有表里value为2、name为“nm”的记录一共有几条,这个SQL语句该怎么写?

...全文
109 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
slowgrace 2009-03-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 jhone99 的回复:]
SELECT SUM(CNUM)
FROM (
select COUNT(*) AS CNUM from 表1 where [name]='nm' and [value]=2
union
select COUNT(*) AS CNUM from 表2 where [name]='nm' and [value]=2
union
select COUNT(*) AS CNUM from 表3 where [name]='nm' and [value]=2
union
select COUNT(*) AS CNUM from 表4 where [name]='nm' and [value]=2
union
select COUNT(*) AS CNUM from 表5 w…
[/Quote]

GOOD。谢谢!
jhone99 2009-03-10
  • 打赏
  • 举报
回复
SELECT SUM(CNUM)
FROM (
select COUNT(*) AS CNUM from 表1 where [name]='nm' and [value]=2
union
select COUNT(*) AS CNUM from 表2 where [name]='nm' and [value]=2
union
select COUNT(*) AS CNUM from 表3 where [name]='nm' and [value]=2
union
select COUNT(*) AS CNUM from 表4 where [name]='nm' and [value]=2
union
select COUNT(*) AS CNUM from 表5 where [name]='nm' and [value]=2
)
jhone99 2009-03-10
  • 打赏
  • 举报
回复
SELECT COUNT(*)
FROM (
select [name],[value],id from 表1 where [name]='nm' and [value]=2
union
select [name],[value],id from 表2 where [name]='nm' and [value]=2
union
select [name],[value],id from 表3 where [name]='nm' and [value]=2
union
select [name],[value],id from 表4 where [name]='nm' and [value]=2
union
select [name],[value],id from 表5 where [name]='nm' and [value]=2
)
slowgrace 2009-03-10
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lingshao0909 的回复:]
引用 1 楼 Leftie 的回复:
select [name],[value],id from 表1 where [name]='nm' and [value]=2
union
select [name],[value],id from 表2 where [name]='nm' and [value]=2
union
select [name],[value],id from 表3 where [name]='nm' and [value]=2
union
select [name],[value],id from 表4 where [name]='nm' and [value]=2
union
select [name],[value],id from 表5 where [name]='nm' and [value]=2

[/Quote]

呵呵,貌似你也没理解对哦。
slowgrace 2009-03-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 Leftie 的回复:]
select [name],[value],id from 表1 where [name]='nm' and [value]=2
union
select [name],[value],id from 表2 where [name]='nm' and [value]=2
union
select [name],[value],id from 表3 where [name]='nm' and [value]=2
union
select [name],[value],id from 表4 where [name]='nm' and [value]=2
union
select [name],[value],id from 表5 where [name]='nm' and [value]=2
[/Quote]

谢谢。不过我是想要知道 总过有几条这样的记录,就是检索结果只是一个数字,表明所有表里这样的记录共有几条。
lingshao0909 2009-03-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 Leftie 的回复:]
select [name],[value],id from 表1 where [name]='nm' and [value]=2
union
select [name],[value],id from 表2 where [name]='nm' and [value]=2
union
select [name],[value],id from 表3 where [name]='nm' and [value]=2
union
select [name],[value],id from 表4 where [name]='nm' and [value]=2
union
select [name],[value],id from 表5 where [name]='nm' and [value]=2
[/Quote]
正解
饮水需思源 2009-03-10
  • 打赏
  • 举报
回复
select [name],[value],id from 表1 where [name]='nm' and [value]=2
union
select [name],[value],id from 表2 where [name]='nm' and [value]=2
union
select [name],[value],id from 表3 where [name]='nm' and [value]=2
union
select [name],[value],id from 表4 where [name]='nm' and [value]=2
union
select [name],[value],id from 表5 where [name]='nm' and [value]=2

1,217

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧