SQL包查询
我在postgreSQL数据库中有一个地块表,需要查询特定地块。3个字段-门、街区、地块SQL包查询,sql,Sql,我在postgreSQL数据库中有一个地块表,需要查询特定地块。3个字段-门、街区、地块 mun1: block 46 and lot 2 mun2: block 1 and lot 1.1 block 2 and lot 6 block 2 and lot 7 block 5 and lot 2 block 11 and lot 1 mun3: block 11 and lot 2 block 11 and lot 2.2
mun1: block 46 and lot 2
mun2: block 1 and lot 1.1
block 2 and lot 6
block 2 and lot 7
block 5 and lot 2
block 11 and lot 1
mun3: block 11 and lot 2
block 11 and lot 2.2
block 7 and lot 2
block 8 and lot 2
我可以一次查询每一个,但我不知道如何在一个查询中完成它们……有没有语法和逻辑方面的建议
例:
像这样的
select * from parcels
where (mun = 'mun1' and block = '46' and lot = '2')
or (mun = 'mun2' and
(block = '1' and lot = '1.1'
or block = '2' and lot = '6'
or block = '2' and lot = '7'
or block = '5' and lot = '2'
or block = '11' and lot = '1'
)
)
or (mun = 'mun3' and
(block = '11' and lot = '2'
or block = '11' and lot = '2.2'
or block = '7' and lot = '2'
or block = '8' and lot = '2'
)
)
你能提供一些样本数据吗?我想你们的问题还不清楚。@GiorgosAltanis check EditOK是的,这就是我想要的逻辑,我将给出一个简单得多的方法,用你们想要的值创建一个临时表/表变量,并进行内部join@MarshallTigerus你愿意分享你的答案吗?
select * from parcels
where (mun = 'mun1' and block = '46' and lot = '2')
or (mun = 'mun2' and
(block = '1' and lot = '1.1'
or block = '2' and lot = '6'
or block = '2' and lot = '7'
or block = '5' and lot = '2'
or block = '11' and lot = '1'
)
)
or (mun = 'mun3' and
(block = '11' and lot = '2'
or block = '11' and lot = '2.2'
or block = '7' and lot = '2'
or block = '8' and lot = '2'
)
)