我正在处理一个有很多连接的查询。返回的数据是正确的:
SELECT
cmb.item_id,
cmba.field_id,
cmba.attr_val,
cmba.upd_dtt,
cmba.upd_usr
FROM contract_member_brg cmb
INNER JOIN contract_member_brg_attr cmba
ON (cmb.item_id = cmba.item_id) where cmb.item_id=8086
UNION
SELECT
cmb.item_id,
ca.field_id,
ca.attr_val,
ca.upd_dtt,
ca.upd_usr
FROM contract_member_brg cmb
INNER JOIN contract_attr ca
ON (cmb.contract_item_id = ca.item_id)
WHERE ca.field_id IN (413) and cmb.item_id=8086
UNION
SELECT
cmb.item_id,
91,
m.item_name,
m.upd_dtt,
m.upd_usr
FROM contract_member_brg cmb
INNER JOIN contract_attr ca
ON (cmb.contract_item_id = ca.item_id
AND ca.field_id = 234)
INNER JOIN mfr m
ON (ca.attr_val = m.item_pk);
我正在尝试过滤特定的item_id:
我已尝试使用不同别名的WHERE子句。
在哪里可以为此查询添加过滤器,以便我只能在此select语句中返回特定的item_id?
答案 0 :(得分:2)
使您的整个查询成为子查询并对其进行过滤:
select * from
(SELECT
cmb.item_id,
cmba.field_id,
cmba.attr_val,
cmba.upd_dtt,
cmba.upd_usr
FROM contract_member_brg cmb
INNER JOIN contract_member_brg_attr cmba
ON (cmb.item_id = cmba.item_id) where cmb.item_id=8086
UNION
SELECT
cmb.item_id,
ca.field_id,
ca.attr_val,
ca.upd_dtt,
ca.upd_usr
FROM contract_member_brg cmb
INNER JOIN contract_attr ca
ON (cmb.contract_item_id = ca.item_id)
WHERE ca.field_id IN (413) and cmb.item_id=8086
UNION
SELECT
cmb.item_id,
91,
m.item_name,
m.upd_dtt,
m.upd_usr
FROM contract_member_brg cmb
INNER JOIN contract_attr ca
ON (cmb.contract_item_id = ca.item_id
AND ca.field_id = 234)
INNER JOIN mfr m
ON (ca.attr_val = m.item_pk)
) a
where item_id = 'whatevs';