简体   繁体   English

如何在mysql中不存在记录时创建检查?

[英]How to create a check if record does not exists in mysql?

Hi I want to show the county in the following query but if county does not exist, return the city instead. 您好我想在以下查询中显示县,但如果县不存在,请返回城市。

        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

Thanks 谢谢

Try using COALESCE() : 尝试使用COALESCE()

COALESCE(county, city) AS location

If county is NULL it will return the value of city instead. 如果countyNULL ,则返回city的值。

So: 所以:

SELECT P.id  AS id, SUM(P.price)  AS price, 
    P.tax, 
    COALESCE(county, city) AS location

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM