我的数据集看起来像这样
parent_id address_type
1 billing
1 shipping
8 billing
429 billing
429 shipping
429 vendor
如何选择具有address_type ='shipping'的行 如果没有address_type'shipping',请选择address_type ='billing'而不是
结果应如下所示:
parent_id address_type
1 shipping
8 billing
429 shipping
非常感谢您提前
答案 0 :(得分:1)
达到此结果的一种方法是使用UNION ALL
SELECT * FROM Table t
WHERE t.address_type = 'shipping'
UNION aLL
SELECT * FROM Table t
WHERE t.address_type = 'billing'
AND NOT EXISTS (SELECT 1 FROM Table t1
WHERE t.id = t1.id AND t1.address_type = 'shipping')
答案 1 :(得分:1)
尝试这样的事情:
select parent_id, address_type
from YourTable
where address_type = 'shipping'
union all
select parent_id, address_type
from YourTable
where address_type = 'billing'
and parent_id not in (select parent_id, address_type
from YourTable
where address_type = 'shipping')
order by parent_id;