一个分组查询.问题. 在线等.

qq497525725 2011-07-19 01:16:13
CREATE
TABLE table_app_use_info
(
ID INT NOT NULL AUTO_INCREMENT,
USE_LOG_CODE VARCHAR(100),--这个相当于外键
APPLICATION_ID INT,
PRIMARY KEY (ID)
)
ENGINE=MyISAM DEFAULT CHARSET=utf8;

表数据
ID USE_LOG_CODE APPLICATION_ID
45 a1 27
44 a1 26
37 a2 27
36 a2 26
10 a3 20
11 a3 21
12 a3 24
15 a3 27
41 a4 27
40 a4 26
39 a4 25
38 a4 24

我给一个条件是一个APPLICATION_ID的集合,需要查询出完全对应的 USE_LOG_CODE

如 条件 (26,27) 应该查出 a1,a2 两条记录
条件 (26) 没有记录
条件 (24,25,,27,26) 查出 a4
就是说USE_LOG_CODE 对应的记录应该和条件完全一样才把这个USE_LOG_CODE返回

希望高手帮忙研究下这个SQL 怎么写.

在线等.
...全文
158 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq497525725 2011-07-19
  • 打赏
  • 举报
回复
小小改了一下.
select USE_LOG_CODE
from table_app_use_info t
where locate(','+ltrim(APPLICATION_ID)+',','24,25,27')>0
group by USE_LOG_CODE having count(1) = (select count(1) from table_app_use_info where USE_LOG_CODE = t.USE_LOG_CODE)
and count(1)=3


好像有风险.先不管了.
AcHerat 2011-07-19
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 qq497525725 的回复:]

charindex 这个MY SQL 没有.
[/Quote]

24楼不是有说用 locate么?
qifachen 2011-07-19
  • 打赏
  • 举报
回复
select USE_LOG_CODE
from table_app_use_info a join <条件集> b
on a.APPLICATION_ID=b.APPLICATION_ID
group by a.USE_LOG_CODE
having count(0)>1

cd731107 2011-07-19
  • 打赏
  • 举报
回复
where ','+@str+',' like '%,'+APPLICATION_ID+',%'
cd731107 2011-07-19
  • 打赏
  • 举报
回复
where ','+@str+'," like '%,'+APPLICATION_ID+',%'
qq497525725 2011-07-19
  • 打赏
  • 举报
回复
charindex 这个MY SQL 没有.
AcHerat 2011-07-19
  • 打赏
  • 举报
回复
把charindex换成locate试试!
AcHerat 2011-07-19
  • 打赏
  • 举报
回复

select distinct USE_LOG_CODE
from table_app_use_info t
where charindex(','+ltrim(APPLICATION_ID)+',',','+/*这个地方写你程序中获取的字符串*/+',')>0
group by USE_LOG_COD
having count(1) = (select count(1) from table_app_use_info where USE_LOG_CODE = t.USE_LOG_CODE);
cd731107 2011-07-19
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 qq497525725 的回复:]
嗯 MY SQL 这个我试了
帮忙改下这里 where charindex(','+ltrim(APPLICATION_ID)+',',','+@str+',')>0
我直接传(26,27)就可以的 我程序动态拼接就行.
[/Quote]
这句你自己不已经写对了吗
qq497525725 2011-07-19
  • 打赏
  • 举报
回复
where charindex(','+ltrim(APPLICATION_ID)+',',','+@str+',')>0
这个不太懂.
qq497525725 2011-07-19
  • 打赏
  • 举报
回复
嗯 MY SQL 这个我试了
帮忙改下这里 where charindex(','+ltrim(APPLICATION_ID)+',',','+@str+',')>0
我直接传(26,27)就可以的 我程序动态拼接就行.
AcHerat 2011-07-19
  • 打赏
  • 举报
回复

--试试这个:
declare @str varchar(8000);
set @str = '26,27';

select distinct USE_LOG_CODE
from table_app_use_info t
where charindex(','+ltrim(APPLICATION_ID)+',',','+@str+',')>0
group by USE_LOG_COD
having count(1) = (select count(1) from table_app_use_info where USE_LOG_CODE = t.USE_LOG_CODE);
qq497525725 2011-07-19
  • 打赏
  • 举报
回复
5555555555 悲剧了.
AcHerat 2011-07-19
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 qq497525725 的回复:]

啊 LS的 对不起呀. 我这个是MYSQL 一般的SQL可以实现么? 不实用MS SQL 特有的东西,
[/Quote]

MYSQL不懂了,很多函数语法顺序和MSSQL的不一样,不知道怎么写。
qq497525725 2011-07-19
  • 打赏
  • 举报
回复
啊 LS的 对不起呀. 我这个是MYSQL 一般的SQL可以实现么? 不实用MS SQL 特有的东西,
AcHerat 2011-07-19
  • 打赏
  • 举报
回复

--这个可以!

CREATE TABLE table_app_use_info
(
ID INT NOT NULL,
USE_LOG_CODE VARCHAR(100),--这个相当于外键
APPLICATION_ID INT
)
insert into table_app_use_info
select 45,'a1',27 union all
select 44,'a1',26 union all
select 37,'a2',27 union all
select 36,'a2',26 union all
select 10,'a3',20 union all
select 11,'a3',21 union all
select 12,'a3',24 union all
select 15,'a3',27 union all
select 41,'a4',27 union all
select 40,'a4',26 union all
select 39,'a4',25 union all
select 38,'a4',24
go

declare @str varchar(8000)
set @str = '26,27'

;with ach as
(
select USE_LOG_CODE,count(1) cnt,
(select count(1) from table_app_use_info where USE_LOG_CODE = t.USE_LOG_CODE) num
from table_app_use_info t
where charindex(','+ltrim(APPLICATION_ID)+',',','+@str+',')>0
group by USE_LOG_CODE
)

select USE_LOG_CODE
from ach
where cnt = num and cnt = len(@str)-len(replace(@str,',',''))+1

drop table table_app_use_info

/************

USE_LOG_CODE
----------------------------------------------------------------------------------------------------
a1
a2

(2 行受影响)

AcHerat 2011-07-19
  • 打赏
  • 举报
回复

CREATE TABLE table_app_use_info
(
ID INT NOT NULL,
USE_LOG_CODE VARCHAR(100),--这个相当于外键
APPLICATION_ID INT
)
insert into table_app_use_info
select 45,'a1',27 union all
select 44,'a1',26 union all
select 37,'a2',27 union all
select 36,'a2',26 union all
select 10,'a3',20 union all
select 11,'a3',21 union all
select 12,'a3',24 union all
select 15,'a3',27 union all
select 41,'a4',27 union all
select 40,'a4',26 union all
select 39,'a4',25 union all
select 38,'a4',24
go

declare @str varchar(8000)
set @str = '24,25,26,27'

;with ach as
(
select USE_LOG_CODE,count(1) cnt
from table_app_use_info
where charindex(','+ltrim(APPLICATION_ID)+',',','+@str+',')>0
group by USE_LOG_CODE
)

select USE_LOG_CODE
from ach
where cnt = len(@str)-len(replace(@str,',',''))+1

drop table table_app_use_info

/*************

USE_LOG_CODE
----------------------------------------------------------------------------------------------------
a4

(1 行受影响)
AcHerat 2011-07-19
  • 打赏
  • 举报
回复

declare @str varchar(8000)
set @str = '26,27'

select distinct USE_LOG_CODE
from table_app_use_info t
where charindex(','+ltrim(APPLICATION_ID)+',',','+@str+',')>0
and (select count(1) from table_app_use_info where USE_LOG_CODE = t.USE_LOG_CODE) = len(@str)-len(replace(@str,',',''))+1
X_0 2011-07-19
  • 打赏
  • 举报
回复

CREATE
TABLE table_app_use_info
(
ID INT NOT NULL,
USE_LOG_CODE VARCHAR(100),--这个相当于外键
APPLICATION_ID INT,
PRIMARY KEY (ID)
)
ENGINE=MyISAM DEFAULT CHARSET=utf8;

insert into table_app_use_info select 45,'a1',27
insert into table_app_use_info select 44 ,'a1', 26
insert into table_app_use_info select 37 ,'a2', 27
insert into table_app_use_info select 36 ,'a2', 26
insert into table_app_use_info select 10 ,'a3', 20
insert into table_app_use_info select 11 ,'a3', 21
insert into table_app_use_info select 12 ,'a3', 24
insert into table_app_use_info select 15 ,'a3', 27
insert into table_app_use_info select 41 ,'a4', 27
insert into table_app_use_info select 40 ,'a4', 26
insert into table_app_use_info select 39 ,'a4', 25
insert into table_app_use_info select 38 ,'a4', 24

;with t as
(
select USE_LOG_CODE,APPLICATION_ID=stuff((select ','+convert(varchar,APPLICATION_ID)
from table_app_use_info t2
where t2.USE_LOG_CODE=t1.USE_LOG_CODE
for xml path('')),1,1,'')
from table_app_use_info t1
group by USE_LOG_CODE
)
select * from t where APPLICATION_ID='24,25,26,27'

/*
USE_LOG_CODE APPLICATION_ID
-------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
a4 24,25,26,27
*/


AcHerat 2011-07-19
  • 打赏
  • 举报
回复

--ID USE_LOG_CODE APPLICATION_ID table_app_use_info

declare @str varchar(8000)
set @str = '26,27'

select USE_LOG_CODE
from table_app_use_info
where charindex(','+ltrim(APPLICATION_ID)+',',','+@str+',')>0
group by USE_LOG_CODE
having count(distinct APPLICATION_ID) = len(@str)-len(replace(@str,',',''))+1
加载更多回复(10)

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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