ID Name
1 1N
2 2N
子表如下:
ID MainTblID Name Selected
1 1 1M 0
2 1 2M 0
3 1 3M 1
4 2 4M 0
5 2 5M 0
我想通过查询主表得出这样的结果
ID Name Selected
1 1N 1
2 2N 0
也就是说selected根据子表对应的记录计算。
4 个解决方案
#1
这个问题有这么难么?一个回答的都没有。
SELECT *,(SELECT count(*) FROM SubTbl WHERE MainTblID=ID AND Selected=1) as Selected FROM MainTbl
这么不就可以了?为啥一个回复都没有?问题太简单了么?
SELECT *,(SELECT count(*) FROM SubTbl WHERE MainTblID=ID AND Selected=1) as Selected FROM MainTbl
这么不就可以了?为啥一个回复都没有?问题太简单了么?
#2
SELECT
a.id, a.NAME, selected=ISNULL(SUM(B.SELECTED), 0)
FROM tbA a
LEFT JOIN tbB b
ON a.id = b.MainTblID
AND b.Selected = 1
GROUP BY a.id, a.name
#3
SELECT
a.id, a.NAME, selected=COUNT(B.Selected)
FROM tbA a
LEFT JOIN tbB b
ON a.id = b.MainTblID
AND b.Selected = 1
GROUP BY a.id, a.NAME
#4
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-08-06 15:51:47
-- Version:
-- Microsoft SQL Server 2014 (CTP1) - 11.0.9120.5 (X64)
-- Jun 10 2013 20:09:10
-- Copyright (c) Microsoft Corporation
-- Enterprise Evaluation Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go
create table [a]([ID] int,[Name] varchar(2))
insert [a]
select 1,'1N' union all
select 2,'2N'
--> 测试数据:[b]
if object_id('[b]') is not null drop table [b]
go
create table [b]([ID] int,[MainTblID] int,[Name] varchar(2),[Selected] int)
insert [b]
select 1,1,'1M',0 union all
select 2,1,'2M',0 union all
select 3,1,'3M',1 union all
select 4,2,'4M',0 union all
select 5,2,'5M',0
--------------开始查询--------------------------
select a.id, a.NAME,MAX(b.selected)selected
from [a] a LEFT JOIN b b ON a.id=b.maintblid
GROUP BY a.id, a.NAME
----------------结果----------------------------
/*
id NAME selected
----------- ---- -----------
1 1N 1
2 2N 0
*/
#1
这个问题有这么难么?一个回答的都没有。
SELECT *,(SELECT count(*) FROM SubTbl WHERE MainTblID=ID AND Selected=1) as Selected FROM MainTbl
这么不就可以了?为啥一个回复都没有?问题太简单了么?
SELECT *,(SELECT count(*) FROM SubTbl WHERE MainTblID=ID AND Selected=1) as Selected FROM MainTbl
这么不就可以了?为啥一个回复都没有?问题太简单了么?
#2
SELECT
a.id, a.NAME, selected=ISNULL(SUM(B.SELECTED), 0)
FROM tbA a
LEFT JOIN tbB b
ON a.id = b.MainTblID
AND b.Selected = 1
GROUP BY a.id, a.name
#3
SELECT
a.id, a.NAME, selected=COUNT(B.Selected)
FROM tbA a
LEFT JOIN tbB b
ON a.id = b.MainTblID
AND b.Selected = 1
GROUP BY a.id, a.NAME
#4
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-08-06 15:51:47
-- Version:
-- Microsoft SQL Server 2014 (CTP1) - 11.0.9120.5 (X64)
-- Jun 10 2013 20:09:10
-- Copyright (c) Microsoft Corporation
-- Enterprise Evaluation Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go
create table [a]([ID] int,[Name] varchar(2))
insert [a]
select 1,'1N' union all
select 2,'2N'
--> 测试数据:[b]
if object_id('[b]') is not null drop table [b]
go
create table [b]([ID] int,[MainTblID] int,[Name] varchar(2),[Selected] int)
insert [b]
select 1,1,'1M',0 union all
select 2,1,'2M',0 union all
select 3,1,'3M',1 union all
select 4,2,'4M',0 union all
select 5,2,'5M',0
--------------开始查询--------------------------
select a.id, a.NAME,MAX(b.selected)selected
from [a] a LEFT JOIN b b ON a.id=b.maintblid
GROUP BY a.id, a.NAME
----------------结果----------------------------
/*
id NAME selected
----------- ---- -----------
1 1N 1
2 2N 0
*/