如果不匹配,则返回某些值的条件

时间:2014-04-08 12:00:33

标签: mysql join inner-join where outer-join

SELECT
  PROJECT_ESTIMATES_ID,
  PIPELINE_STEP_ID
FROM
  PROJECT_ESTIMATES
WHERE
  PROJECT_BASIC_INFORMATION_ID='29ab9760-c75b-4479-882c-bc84426d55ac';

这给出了以下输出

enter image description here

使用where where where class,

SELECT
  PE.PROJECT_ESTIMATES_ID,
  MPS.STEP_DESCRIPTION
FROM
  PROJECT_ESTIMATES PE,
  MST_PIPELINE_STEPS MPS 
WHERE
  PE.`PROJECT_BASIC_INFORMATION_ID` ='29ab9760-c75b-4479-882c-bc84426d55ac' 
  AND PE.`TENANT_ID`='{0559cdcb-c63b-4c81-be91-b78Tenant1000'
  AND PE.PIPELINE_STEP_ID=MPS.PIPELINE_STEP_ID
ORDER BY
  PE.MODIFIED_DATE DESC;

给出以下输出。

enter image description here

我想将第一条记录的step_description为0,其中PROJECT_ESTIMATES_ID值为046a190e-a895-4ce2-bb10-7a583d648b99.ie,下图显示了所需的输出。 enter image description here

2 个答案:

答案 0 :(得分:0)

如果我没有误解你的问题,这将有效:

SELECT 
PE.PROJECT_ESTIMATES_ID,
IF(
PE.PROJECT_ESTIMATES_ID = '046a190e-a895-4ce2-bb10-7a583d648b99', 
0,
PE.PROJECT_ESTIMATES_ID) AS STEP_DESCRIPTION
FROM 
PROJECT_ESTIMATES PE, 
MST_PIPELINE_STEPS MPS 
WHERE PE.`PROJECT_BASIC_INFORMATION_ID` ='29ab9760-c75b-4479-882c-bc84426d55ac' 
AND PE.`TENANT_ID`='{0559cdcb-c63b-4c81-be91-b78Tenant1000' 
AND PE.PIPELINE_STEP_ID=MPS.PIPELINE_STEP_ID 
ORDER BY PE.MODIFIED_DATE DESC

除非你的意思是你想要零而不是空白或null,在这种情况下这就是你想要的:

SELECT 
PE.PROJECT_ESTIMATES_ID
IF(
LENGTH
(ISNULL(
PE.PROJECT_ESTIMATES_ID,'')) > 0,
PE.PROJECT_ESTIMATES_ID, 0) AS STEP_DESCRIPTION
FROM PROJECT_ESTIMATES PE, 
     MST_PIPELINE_STEPS MPS 
WHERE PE.`PROJECT_BASIC_INFORMATION_ID` ='29ab9760-c75b-4479-882c-bc84426d55ac' 
AND PE.`TENANT_ID`='{0559cdcb-c63b-4c81-be91-b78Tenant1000' 
AND PE.PIPELINE_STEP_ID=MPS.PIPELINE_STEP_ID 
ORDER BY PE.MODIFIED_DATE DESC

答案 1 :(得分:0)

此查询符合我的要求。

SELECT PE.PROJECT_ESTIMATES_ID, MPS.STEP_DESCRIPTION
FROM PROJECT_ESTIMATES PE
LEFT JOIN MST_PIPELINE_STEPS MPS
ON PE.PIPELINE_STEP_ID=MPS.PIPELINE_STEP_ID WHERE
PE.`PROJECT_BASIC_INFORMATION_ID` ='29ab9760-c75b-4479-882c-bc84426d55ac'
AND PE.`TENANT_ID`='{0559cdcb-c63b-4c81-be91-b78Tenant1000' 
ORDER BY PE.MODIFIED_DATE DESC ;