0

嗨,我想在以下查询中显示县,但如果县不存在,则返回城市。

        SELECT P.id  AS id, SUM(P.price)  AS price, 
                    P.tax, 
                    county  
                FROM Purchases AS  P
                JOIN Status  AS S       ON S.id = T.status_id
                JOIN Shipping AS Ship   ON S.shipping_id= Ship.id
                JOIN Shipping_Addresses AS  SA      ON Ship.shipping_address_id = SA.id
                JOIN Zip_Codes          AS ZIP      ON ZIP.zip_code = SA.zip_code
                JOIN Counties           AS C        ON ZIP.city = C.city
                JOIN States             AS STATE    ON STATE.id = C.county_state_id

                GROUP BY ZIP.zip_code
                ORDER BY county

谢谢

4

1 回答 1

2

尝试使用COALESCE()

COALESCE(county, city) AS location

如果countyNULL,它将返回值city而不是。

所以:

SELECT P.id  AS id, SUM(P.price)  AS price, 
    P.tax, 
    COALESCE(county, city) AS location
于 2013-10-25T15:44:33.333 回答