Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL-如果不存在,返回0_Mysql - Fatal编程技术网

MySQL-如果不存在,返回0

MySQL-如果不存在,返回0,mysql,Mysql,现在我有一个非常大的查询,看起来像: $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_tr

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

    $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";
我有一个小问题,我的最后一个
左连接
查询并不总是返回
org.name
(这是短列表,所以不是所有ID都在那里)。 但是,如果
org.name
不存在,我想返回类似
0
的内容

有人能帮助我如何在不破坏
左连接
逻辑的情况下执行此操作吗?

非常感谢。
问候

Andurit

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

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

使用
合并功能:

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
选择c.vin、c.case\u id、c.claimnumber、c.platenumber、c.axrmrs\u id、c.Insurance\u memberid、c.country、c.date\u created、c.Totaloss、c.lastcalc\u Manufactor\u code、c.lastcalc\u model\u code、c.lastcalc\u submodel\u code、c.audavin\u triggered、c.Confect\u日期、c.Factory\u日期、c.manufacturing\u年份,
cl.备件、cl.总成本、cl.工时、cl.人工成本、cl.计算日期、cl.油漆人工、cl.油漆材料、cl.货币、汽车制造商、汽车模型、汽车子模型、,

合并(org.name,0)作为名称--使用
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
选择c.vin、c.case\u id、c.claimnumber、c.platenumber、c.axrmrs\u id、c.Insurance\u memberid、c.country、c.date\u created、c.Totaloss、c.lastcalc\u Manufactor\u code、c.lastcalc\u model\u code、c.lastcalc\u submodel\u code、c.audavin\u triggered、c.Confect\u日期、c.Factory\u日期、c.manufacturing\u年份,
cl.备件、cl.总成本、cl.工时、cl.人工成本、cl.计算日期、cl.油漆人工、cl.油漆材料、cl.货币、汽车制造商、汽车模型、汽车子模型、,

COALESCE(org.name,0)作为名称--hey@mikeb谢谢你的回答,它很完美,所以你必须竖起大拇指,但是Roberto是第一个保持公平的人,所以我认为他的答案是正确的。hey@mikeb感谢你的回答,它很完美,所以你必须竖起大拇指,但是Roberto是第一个保持公平的人,所以我认为他的答案是正确的。