每个id选择一行,条件在nonid字段中Magento选择送货地址

时间:2014-02-13 16:48:32

标签: mysql sql magento

我的数据集看起来像这样

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

非常感谢您提前

2 个答案:

答案 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;