我有这个查询,它是基于条件的多个选择的并集。 问题是“AND”部分仅适用于最后一个选择,但我想将它应用于整个选择(所有联盟)
select * from
(select con.id_contact as v,substr(concat(ifnull(con.firstname,''),ifnull(con.lastname,'')),1,4) as s0, null as s1, null as s2 from contact con, fu_rec_group_link g
where con.id_contact = g.id_rec and id_group=125801) as tbl244295 where 1=1 UNION
select * from
(select con.id_contact as v,substr(concat(ifnull(con.firstname,''),ifnull(con.lastname,'')),1,4) as s0, null as s1, null as s2 from contact con, fu_rec_group_link g where
con.id_contact = g.id_rec and id_group=125803) as tbl244296 where 1=1 UNION
select * from
(select con.id_contact as v,substr(concat(ifnull(con.firstname,''),ifnull(con.lastname,'')),1,4) as s0, null as s1, null as s2 from contact con, fu_rec_group_link g where
con.id_contact = g.id_rec and id_group=126270) as tbl244297 where 1=1 AND v NOT IN
(select v from (select * from (select con.id_contact as v,substr(concat(ifnull(con.firstname,''),ifnull(con.lastname,'')),1,4) as s0, null as s1, null as s2 from contact con
where id_user=11944 and unregistered is not null) as tbl244299 where 1=1) as tblMinus )
答案 0 :(得分:2)
正如@jarlh所指出的,对原始查询的修复是将联合包装在另一个选择中并将最后一个where条件应用于此。此外,当您使用union
而不使用all
时,您可以消除重复项,但这种方式可能会很昂贵;最好指定union all
并使用distinct
或group by
子句来消除重复的行。
但是......看看你的查询它似乎可以简化得很简单并简化为下面的查询,它应该呈现相同的结果但表现更好。
select distinct
con.id_contact as v,
substr(concat(ifnull(con.firstname,''),ifnull(con.lastname,'')),1,4) as s0,
null as s1,
null as s2
from contact con
join fu_rec_group_link g on con.id_contact = g.id_rec
where id_group in (125801, 125803, 126270)
and con.id_contact not in (
select con.id_contact as v
from contact con
where id_user=11944 and unregistered is not null
)
由于既没有关于表格设计的任何信息,也没有任何用我的假设进行测试的数据可能完全不正确,如果是这样,我会立即删除答案。
答案 1 :(得分:0)
在派生表中执行WHERE
,将select * from
(
select ...
union
select ...
) dt
where dt.col ...
置于其外部:
"P272": [
{
"id": "q24871$4721C959-0FCF-49D4-9265-E4FAC217CB6E",
"mainsnak": {
"snaktype": "value",
"property": "P272",
"datatype": "wikibase-item",
"datavalue": {
"value": {
"entity-type": "item",
"numeric-id": 775450
},
"type": "wikibase-entityid"
}
},
"type": "statement",
"rank": "normal"
},
{
"id": "q24871$31777445-1068-4C38-9B4B-96362577C442",
"mainsnak": {
"snaktype": "value",
"property": "P272",
"datatype": "wikibase-item",
"datavalue": {
"value": {
"entity-type": "item",
"numeric-id": 3041294
},
"type": "wikibase-entityid"
}
},
"type": "statement",
"rank": "normal"
},
{
"id": "q24871$08009F7A-8E54-48C3-92D9-75DEF4CF3E8D",
"mainsnak": {
"snaktype": "value",
"property": "P272",
"datatype": "wikibase-item",
"datavalue": {
"value": {
"entity-type": "item",
"numeric-id": 646968
},
"type": "wikibase-entityid"
}
},
"type": "statement",
"rank": "normal"
},
{
"id": "q24871$CA53B5EB-1041-4701-A36E-7C348FAC984E",
"mainsnak": {
"snaktype": "value",
"property": "P272",
"datatype": "wikibase-item",
"datavalue": {
"value": {
"entity-type": "item",
"numeric-id": 434841
},
"type": "wikibase-entityid"
}
},
"type": "statement",
"rank": "normal",
"references": [
{
"hash": "50f57a3dbac4708ce4ae4a827c0afac7fcdb4a5c",
"snaks": {
"P143": [
{
"snaktype": "value",
"property": "P143",
"datatype": "wikibase-item",
"datavalue": {
"value": {
"entity-type": "item",
"numeric-id": 11920
},
"type": "wikibase-entityid"
}
}
]
},
"snaks-order": [
"P143"
]
}
]
}
],