MySQL - 如果NOT EXIST返回0

时间:2015-11-30 15:59:37

标签: mysql

现在我有一个非常大的查询,看起来像:

var $el = '<svg xmlns="http://www.w3.org/2000/svg" height="367" width="780"id="canvas" version="1.1" style="overflow: hidden;">
<circle r="12" style="cursor: move; fill: #4682b4; stroke: #000000; stroke-width: 1.5px;"/></svg>';

var serializer = new XMLSerializer();
var res = serializer.serializeToString($el);

var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:image/svg+xml;base64,' + btoa(res);
hiddenElement.download = 'export.svg';
hiddenElement.click();

我有一个小问题,我的上一个 $sql = "SELECT c.vin, c.case_id, c.claimnumber, c.platenumber, c.axrmrs_id, c.insurer_memberid, c.country, c.date_created, c.totalloss, c.lastcalc_manufacturer_code, c.lastcalc_model_code, c.lastcalc_submodel_code, c.audavin_triggered, c.accident_date, c.registration_date, c.manufacturing_year, cl.spareparts, cl.totalcosts, cl.laborhours, cl.laborcosts, cl.calculationdate, cl.paintlabor, cl.paintmaterial, cl.currency, car.manufacturer, car.model, car.submodel, org.name GROUP_CONCAT(DISTINCT IF(po.repairmethod LIKE 'L%',po.text,NULL) ORDER BY 1) AS textL, GROUP_CONCAT(DISTINCT IF(po.repairmethod LIKE 'E',po.text,NULL) ORDER BY 1) AS textE FROM axnmrs_cases AS c LEFT JOIN axnmrs_calculations as cl on c.case_id = cl.case_id AND c.country = cl.country LEFT JOIN axnmrs_positions as po on c.case_id = po.case_id LEFT JOIN car_type as car on car.manufacturer_code = c.lastcalc_manufacturer_code AND car.main_type = c.lastcalc_model_code AND car.subtype_code = c.lastcalc_submodel_code LEFT JOIN organization_list as org on org.memberId = c.insurer_memberid WHERE c.vin= :vin GROUP BY c.vin, c.case_id, c.axrmrs_id"; QUERY并不总是返回LEFT JOIN(这是短名单所以并非所有ID都在那里)。 但是,如果org.name不存在,我希望返回0之类的内容。

有人可以帮助我如何在不违反org.name逻辑的情况下这样做吗?

谢谢
亲切的问候
Andurit

2 个答案:

答案 0 :(得分:2)

在您的选择中使用IFNULL(org.name, 0)

答案 1 :(得分:2)

使用coalesce功能:

SELECT c.vin, c.case_id, c.claimnumber, c.platenumber, c.axrmrs_id, c.insurer_memberid, c.country, c.date_created, c.totalloss, c.lastcalc_manufacturer_code, c.lastcalc_model_code, c.lastcalc_submodel_code, c.audavin_triggered, c.accident_date, c.registration_date, c.manufacturing_year,
                  cl.spareparts, cl.totalcosts, cl.laborhours, cl.laborcosts, cl.calculationdate, cl.paintlabor, cl.paintmaterial, cl.currency, car.manufacturer, car.model, car.submodel, 

                  COALESCE(org.name, 0) as name -- <--- Check this !!!

                  GROUP_CONCAT(DISTINCT IF(po.repairmethod LIKE 'L%',po.text,NULL) ORDER BY 1) AS textL,
                  GROUP_CONCAT(DISTINCT IF(po.repairmethod LIKE 'E',po.text,NULL) ORDER BY 1) AS textE
                FROM axnmrs_cases AS c
                  LEFT JOIN axnmrs_calculations as cl on c.case_id = cl.case_id AND c.country = cl.country
                  LEFT JOIN axnmrs_positions as po on c.case_id = po.case_id
                  LEFT JOIN car_type as car on car.manufacturer_code = c.lastcalc_manufacturer_code AND car.main_type = c.lastcalc_model_code AND car.subtype_code = c.lastcalc_submodel_code
                  LEFT JOIN organization_list as org on org.memberId = c.insurer_memberid
                WHERE c.vin= :vin
                  GROUP BY c.vin, c.case_id, c.axrmrs_id

您也可以使用IFNULL代替COALESCE,在这种情况下它们是相同的。它们返回第一个非null的参数。例如coalesce(null,5) = 5,coalesce(3, 5) = 3。 isnull和coalesce之间的区别在于coalesce可以接收更多参数。