sql语句怎么写

fanbingyuan 2011-12-05 02:39:23
根据表a中的某字段的不同 分别像其他不同的表中取数据
比如

表 A
a b
1 B
2 C
表b
c d
5 6
5 7
5 8

表c
e f
6 8
7 9
10 11

结果集合
表 d
a b count
1 B 1=b表中d列小于7的个数
2 C 2=c表中f列小于11的个数
...全文
192 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
fanbingyuan 2011-12-06
  • 打赏
  • 举报
回复
换了种方式是解决了,但是这种的写法没得写么
美到心痛 2011-12-06
  • 打赏
  • 举报
回复
抱歉,不會寫.等高手吧~
美到心痛 2011-12-05
  • 打赏
  • 举报
回复
要用動態語句.我想想
fanbingyuan 2011-12-05
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 sjcss 的回复:]
if判斷的條件大概是什麽
[/Quote]
如果a表中的b字段是B就从b表中取
如果是C 就从c表中取
美到心痛 2011-12-05
  • 打赏
  • 举报
回复
if判斷的條件大概是什麽
fanbingyuan 2011-12-05
  • 打赏
  • 举报
回复
救命啊
fanbingyuan 2011-12-05
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 pengxuan 的回复:]
SQL code


if object_id('A') is not null
drop table A
go
create table A
(
a int,
b varchar(10)
)
go
insert into A
select 1,'B' union all
select 2,'C'
go
if object_id('B') is not n……
[/Quote]

是这个意思,不过 不确定的是从B表还是从C表中取数据,我就差一个if判断了 但是又不知道该如何判断
pengxuan 2011-12-05
  • 打赏
  • 举报
回复

if object_id('A') is not null
drop table A
go
create table A
(
a int,
b varchar(10)
)
go
insert into A
select 1,'B' union all
select 2,'C'
go
if object_id('B') is not null
drop table B
go
create table B
(
c int,
d int
)
go
insert into B
select 5,6 union all
select 5,7 UNION ALL
select 5,8
go
if object_id('C') is not null
drop table C
go
create table C
(
e int,
f int
)
go
insert into C
select 6,8 union all
select 7,9 union all
select 10,11
go
select a,b,count=(select count(*) from B where d<7) from a where a=1
union all
select a,b,count=(select count(*) from C where f<11) from a where a=2
go
/*
a b count
----------- ---------- -----------
1 B 1
2 C 2

(2 行受影响)
*/
fanbingyuan 2011-12-05
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 down_login 的回复:]
没有看懂问题
[/Quote]
b表和c表存储并列的两套数据,比如身高和体重,
a表统计身高和体重满足某种条件的数量

a的格式wei
ID Type NUM
1 身高 数量
2 体重 数量
fanbingyuan 2011-12-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fredrickhu 的回复:]
没有看明白意思 C表起什么作用啊?
[/Quote]
b表和c表分别存储不同类型的并列的两套数据,比如身高和体重
好人吗 2011-12-05
  • 打赏
  • 举报
回复
没有看懂问题
geniuswjt 2011-12-05
  • 打赏
  • 举报
回复
先统计再列转行,如果就2条的话,直接写也行
--小F-- 2011-12-05
  • 打赏
  • 举报
回复
没有看明白意思 C表起什么作用啊?

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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