这个怎么会查不出来?

WormJan 2017-03-19 08:23:35
表A:
aid type value
1 1 12
1 1 13
1 2 6
2 1 0
2 2 0
3 1 15



如果 type是1,value是12, 或者 type是1,value是13, 或者type是1 value是0
如果 type是2,value是0 或者 type是2,value是6


都允许查出来

我写了一个,没有发现哪里有问题,但是查不出来 :

select * from A where ((type = 1 and value = 0) or (type = 1 and value=13) or (type = 1 and value=12)) and ((type = 2 and value = 0) or (type = 2 and value = 6))

请大侠指点,谢谢
...全文
268 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
WormJan 2017-04-11
  • 打赏
  • 举报
回复
引用 4 楼 sinat_28984567 的回复:
[quote=引用 3 楼 superfans98 的回复:] type=1 value=13 的查不出来。 这个关系,有点没说明白。 type=1的时候,因为访客的1的值可能有3个。所以type=1的时候,value可能有多个,都符合条件,并且,同时还要满足当type=2的时候,value的多个可能的值。 也就是说type=1时,访客的value的值只要有一个匹配就符合要求,但是,还要同时符合type=2时的条件。
--测试数据
if not object_id(N'Tempdb..#T') is null
	drop table #T
Go
Create table #T([aid] int,[type] int,[value] int)
Insert #T
select 1,1,12 union all
select 1,1,13 union all
select 1,2,6 union all
select 2,1,0 union all
select 2,2,0 union all
select 3,1,15
GO

if not object_id(N'Tempdb..#article') is null
	drop table #article
Go
Create table #article([aid] INT,title NVARCHAR(100))
Insert #article
select 1,'文章1' UNION
SELECT 2,'文章2' UNION
SELECT 3,'文章3'
Go
--测试数据结束
SELECT  DISTINCT a.*
FROM    #article a
        JOIN #T b ON b.aid = a.aid
        JOIN #T c ON c.aid = a.aid
WHERE   b.type = 1
        AND ','+'1,12,13'+',' LIKE '%,'+RTRIM(b.value) + '%,'			--这里改成这样的,下边那个也是
        AND c.type = 2
        AND c.value = 6;			--去读职业为6的 ,同上边一样
[/quote] 谢谢,结帖
二月十六 2017-03-20
  • 打赏
  • 举报
回复
引用 3 楼 superfans98 的回复:
type=1 value=13 的查不出来。 这个关系,有点没说明白。 type=1的时候,因为访客的1的值可能有3个。所以type=1的时候,value可能有多个,都符合条件,并且,同时还要满足当type=2的时候,value的多个可能的值。 也就是说type=1时,访客的value的值只要有一个匹配就符合要求,但是,还要同时符合type=2时的条件。
--测试数据
if not object_id(N'Tempdb..#T') is null
	drop table #T
Go
Create table #T([aid] int,[type] int,[value] int)
Insert #T
select 1,1,12 union all
select 1,1,13 union all
select 1,2,6 union all
select 2,1,0 union all
select 2,2,0 union all
select 3,1,15
GO

if not object_id(N'Tempdb..#article') is null
	drop table #article
Go
Create table #article([aid] INT,title NVARCHAR(100))
Insert #article
select 1,'文章1' UNION
SELECT 2,'文章2' UNION
SELECT 3,'文章3'
Go
--测试数据结束
SELECT  DISTINCT a.*
FROM    #article a
        JOIN #T b ON b.aid = a.aid
        JOIN #T c ON c.aid = a.aid
WHERE   b.type = 1
        AND ','+'1,12,13'+',' LIKE '%,'+RTRIM(b.value) + '%,'			--这里改成这样的,下边那个也是
        AND c.type = 2
        AND c.value = 6;			--去读职业为6的 ,同上边一样
WormJan 2017-03-19
  • 打赏
  • 举报
回复
引用 2 楼 sinat_28984567 的回复:
我用mssql写了个例子,你看看
--测试数据
if not object_id(N'Tempdb..#T') is null
	drop table #T
Go
Create table #T([aid] int,[type] int,[value] int)
Insert #T
select 1,1,12 union all
select 1,1,13 union all
select 1,2,6 union all
select 2,1,0 union all
select 2,2,0 union all
select 3,1,15
GO

if not object_id(N'Tempdb..#article') is null
	drop table #article
Go
Create table #article([aid] INT,title NVARCHAR(100))
Insert #article
select 1,'文章1' UNION
SELECT 2,'文章2' UNION
SELECT 3,'文章3'
Go
--测试数据结束
SELECT  a.*
FROM    #article a
        JOIN #T b ON b.aid = a.aid
        JOIN #T c ON c.aid = a.aid
WHERE   b.type = 1
        AND b.value = 12			--读取兴趣为12的,这个12应该是传入进来的,根据当前访问人
        AND c.type = 2
        AND c.value = 6;			--去读职业为6的 ,同上边一样
type=1 value=13 的查不出来。 这个关系,有点没说明白。 type=1的时候,因为访客的1的值可能有3个。所以type=1的时候,value可能有多个,都符合条件,并且,同时还要满足当type=2的时候,value的多个可能的值。 也就是说type=1时,访客的value的值只要有一个匹配就符合要求,但是,还要同时符合type=2时的条件。
二月十六 2017-03-19
  • 打赏
  • 举报
回复
我用mssql写了个例子,你看看
--测试数据
if not object_id(N'Tempdb..#T') is null
	drop table #T
Go
Create table #T([aid] int,[type] int,[value] int)
Insert #T
select 1,1,12 union all
select 1,1,13 union all
select 1,2,6 union all
select 2,1,0 union all
select 2,2,0 union all
select 3,1,15
GO

if not object_id(N'Tempdb..#article') is null
	drop table #article
Go
Create table #article([aid] INT,title NVARCHAR(100))
Insert #article
select 1,'文章1' UNION
SELECT 2,'文章2' UNION
SELECT 3,'文章3'
Go
--测试数据结束
SELECT  a.*
FROM    #article a
        JOIN #T b ON b.aid = a.aid
        JOIN #T c ON c.aid = a.aid
WHERE   b.type = 1
        AND b.value = 12			--读取兴趣为12的,这个12应该是传入进来的,根据当前访问人
        AND c.type = 2
        AND c.value = 6;			--去读职业为6的 ,同上边一样
WormJan 2017-03-19
  • 打赏
  • 举报
回复
type 1 和 2 要求同时满足条件才能查询出来

56,940

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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