如何在mysql中检查负值,然后决定输出
我使用一个mysql案例进行一些计算,我的要求是,如果输出为负数,那么我将在负数列中显示0,否则显示正值 让我来提出这个问题:如何在mysql中检查负值,然后决定输出,mysql,sql,Mysql,Sql,我使用一个mysql案例进行一些计算,我的要求是,如果输出为负数,那么我将在负数列中显示0,否则显示正值 让我来提出这个问题: select concat(jo.title,' (', CCP.name, ')'), PL.analyst, PL.consultant, PL.csm, (CASE WHEN PL.productType like 'Staffing' THEN (SELECT ((DATEDIFF(PL.dateEnd, PL.
select
concat(jo.title,' (', CCP.name, ')'), PL.analyst, PL.consultant,
PL.csm,
(CASE
WHEN PL.productType like 'Staffing' THEN (SELECT ((DATEDIFF(PL.dateEnd, PL.dateClientEffective)) -((WEEK(PL.dateEnd) - WEEK(PL.dateClientEffective)) * 2) - (case when weekday(PL.dateEnd) = 6 then 1 else 0 end) - (case when weekday(PL.dateClientEffective) = 5 then 1 else 0 end)) as DifD) * 8 * (PL.clientBillRate-PL.payRate) ELSE (PL.salary*PL.fee)END) Value
现在,如果值为负值,则应显示0,否则将显示原始值请检查此项,尚未测试 注意:这可能不是最好的解决方案
select concat(jo.title,' (', CCP.name, ')'), PL.analyst, PL.consultant, PL.csm,
if(
(CASE WHEN PL.productType like 'Staffing' THEN (SELECT ((DATEDIFF(PL.dateEnd, PL.dateClientEffective)) -((WEEK(PL.dateEnd) - WEEK(PL.dateClientEffective)) * 2) -
(case when weekday(PL.dateEnd) = 6 then 1 else 0 end) -
(case when weekday(PL.dateClientEffective) = 5 then 1 else 0 end)) as DifD) * 8 * (PL.clientBillRate-PL.payRate) ELSE (PL.salary*PL.fee)END)
< 0,0,
(CASE WHEN PL.productType like 'Staffing' THEN (SELECT ((DATEDIFF(PL.dateEnd, PL.dateClientEffective)) -((WEEK(PL.dateEnd) - WEEK(PL.dateClientEffective)) * 2) -
(case when weekday(PL.dateEnd) = 6 then 1 else 0 end) -
(case when weekday(PL.dateClientEffective) = 5 then 1 else 0 end)) as DifD) * 8 * (PL.clientBillRate-PL.payRate) ELSE (PL.salary*PL.fee)END)
) Value
选择concat(职务名称“(”,CCP.name“))、PL.analyst、PL.consultant、PL.csm、,
如果(
(如果PL.productType类似“人员配置”,则选择((DATEDIFF(PL.dateEnd,PL.dateClientEffective))-((WEEK(PL.dateEnd)-WEEK(PL.dateClientEffective))*2)
(当工作日(PL.dateEnd)=6时,则为1,否则为0结束)——
(当工作日(PL.dateClientEffective)=5然后1否则0结束)作为DifD)*8*(PL.clientBillRate-PL.payRate)否则(PL.salary*PL.fee)结束)
< 0,0,
(如果PL.productType类似“人员配置”,则选择((DATEDIFF(PL.dateEnd,PL.dateClientEffective))-((WEEK(PL.dateEnd)-WEEK(PL.dateClientEffective))*2)
(当工作日(PL.dateEnd)=6时,则为1,否则为0结束)——
(当工作日(PL.dateClientEffective)=5然后1否则0结束)作为DifD)*8*(PL.clientBillRate-PL.payRate)否则(PL.salary*PL.fee)结束)
)价值观
请检查此项,尚未测试
注意:这可能不是最好的解决方案
select concat(jo.title,' (', CCP.name, ')'), PL.analyst, PL.consultant, PL.csm,
if(
(CASE WHEN PL.productType like 'Staffing' THEN (SELECT ((DATEDIFF(PL.dateEnd, PL.dateClientEffective)) -((WEEK(PL.dateEnd) - WEEK(PL.dateClientEffective)) * 2) -
(case when weekday(PL.dateEnd) = 6 then 1 else 0 end) -
(case when weekday(PL.dateClientEffective) = 5 then 1 else 0 end)) as DifD) * 8 * (PL.clientBillRate-PL.payRate) ELSE (PL.salary*PL.fee)END)
< 0,0,
(CASE WHEN PL.productType like 'Staffing' THEN (SELECT ((DATEDIFF(PL.dateEnd, PL.dateClientEffective)) -((WEEK(PL.dateEnd) - WEEK(PL.dateClientEffective)) * 2) -
(case when weekday(PL.dateEnd) = 6 then 1 else 0 end) -
(case when weekday(PL.dateClientEffective) = 5 then 1 else 0 end)) as DifD) * 8 * (PL.clientBillRate-PL.payRate) ELSE (PL.salary*PL.fee)END)
) Value
选择concat(职务名称“(”,CCP.name“))、PL.analyst、PL.consultant、PL.csm、,
如果(
(如果PL.productType类似“人员配置”,则选择((DATEDIFF(PL.dateEnd,PL.dateClientEffective))-((WEEK(PL.dateEnd)-WEEK(PL.dateClientEffective))*2)
(当工作日(PL.dateEnd)=6时,则为1,否则为0结束)——
(当工作日(PL.dateClientEffective)=5然后1否则0结束)作为DifD)*8*(PL.clientBillRate-PL.payRate)否则(PL.salary*PL.fee)结束)
< 0,0,
(如果PL.productType类似“人员配置”,则选择((DATEDIFF(PL.dateEnd,PL.dateClientEffective))-((WEEK(PL.dateEnd)-WEEK(PL.dateClientEffective))*2)
(当工作日(PL.dateEnd)=6时,则为1,否则为0结束)——
(当工作日(PL.dateClientEffective)=5然后1否则0结束)作为DifD)*8*(PL.clientBillRate-PL.payRate)否则(PL.salary*PL.fee)结束)
)价值观
请检查此项,尚未测试
注意:这可能不是最好的解决方案
select concat(jo.title,' (', CCP.name, ')'), PL.analyst, PL.consultant, PL.csm,
if(
(CASE WHEN PL.productType like 'Staffing' THEN (SELECT ((DATEDIFF(PL.dateEnd, PL.dateClientEffective)) -((WEEK(PL.dateEnd) - WEEK(PL.dateClientEffective)) * 2) -
(case when weekday(PL.dateEnd) = 6 then 1 else 0 end) -
(case when weekday(PL.dateClientEffective) = 5 then 1 else 0 end)) as DifD) * 8 * (PL.clientBillRate-PL.payRate) ELSE (PL.salary*PL.fee)END)
< 0,0,
(CASE WHEN PL.productType like 'Staffing' THEN (SELECT ((DATEDIFF(PL.dateEnd, PL.dateClientEffective)) -((WEEK(PL.dateEnd) - WEEK(PL.dateClientEffective)) * 2) -
(case when weekday(PL.dateEnd) = 6 then 1 else 0 end) -
(case when weekday(PL.dateClientEffective) = 5 then 1 else 0 end)) as DifD) * 8 * (PL.clientBillRate-PL.payRate) ELSE (PL.salary*PL.fee)END)
) Value
选择concat(职务名称“(”,CCP.name“))、PL.analyst、PL.consultant、PL.csm、,
如果(
(如果PL.productType类似“人员配置”,则选择((DATEDIFF(PL.dateEnd,PL.dateClientEffective))-((WEEK(PL.dateEnd)-WEEK(PL.dateClientEffective))*2)
(当工作日(PL.dateEnd)=6时,则为1,否则为0结束)——
(当工作日(PL.dateClientEffective)=5然后1否则0结束)作为DifD)*8*(PL.clientBillRate-PL.payRate)否则(PL.salary*PL.fee)结束)
< 0,0,
(如果PL.productType类似“人员配置”,则选择((DATEDIFF(PL.dateEnd,PL.dateClientEffective))-((WEEK(PL.dateEnd)-WEEK(PL.dateClientEffective))*2)
(当工作日(PL.dateEnd)=6时,则为1,否则为0结束)——
(当工作日(PL.dateClientEffective)=5然后1否则0结束)作为DifD)*8*(PL.clientBillRate-PL.payRate)否则(PL.salary*PL.fee)结束)
)价值观
请检查此项,尚未测试
注意:这可能不是最好的解决方案
select concat(jo.title,' (', CCP.name, ')'), PL.analyst, PL.consultant, PL.csm,
if(
(CASE WHEN PL.productType like 'Staffing' THEN (SELECT ((DATEDIFF(PL.dateEnd, PL.dateClientEffective)) -((WEEK(PL.dateEnd) - WEEK(PL.dateClientEffective)) * 2) -
(case when weekday(PL.dateEnd) = 6 then 1 else 0 end) -
(case when weekday(PL.dateClientEffective) = 5 then 1 else 0 end)) as DifD) * 8 * (PL.clientBillRate-PL.payRate) ELSE (PL.salary*PL.fee)END)
< 0,0,
(CASE WHEN PL.productType like 'Staffing' THEN (SELECT ((DATEDIFF(PL.dateEnd, PL.dateClientEffective)) -((WEEK(PL.dateEnd) - WEEK(PL.dateClientEffective)) * 2) -
(case when weekday(PL.dateEnd) = 6 then 1 else 0 end) -
(case when weekday(PL.dateClientEffective) = 5 then 1 else 0 end)) as DifD) * 8 * (PL.clientBillRate-PL.payRate) ELSE (PL.salary*PL.fee)END)
) Value
选择concat(职务名称“(”,CCP.name“))、PL.analyst、PL.consultant、PL.csm、,
如果(
(如果PL.productType类似“人员配置”,则选择((DATEDIFF(PL.dateEnd,PL.dateClientEffective))-((WEEK(PL.dateEnd)-WEEK(PL.dateClientEffective))*2)
(当工作日(PL.dateEnd)=6时,则为1,否则为0结束)——
(当工作日(PL.dateClientEffective)=5然后1否则0结束)作为DifD)*8*(PL.clientBillRate-PL.payRate)否则(PL.salary*PL.fee)结束)
< 0,0,
(如果PL.productType类似“人员配置”,则选择((DATEDIFF(PL.dateEnd,PL.dateClientEffective))-((WEEK(PL.dateEnd)-WEEK(PL.dateClientEffective))*2)
(当工作日(PL.dateEnd)=6时,则为1,否则为0结束)——
(当工作日(PL.dateClientEffective)=5然后1否则0结束)作为DifD)*8*(PL.clientBillRate-PL.payRate)否则(PL.salary*PL.fee)结束)
)价值观
只需这样做:
select temp.* , --all your columns
if(temp.value<0,0,temp.value) as Value
from
( your query mentioned above) temp;
选择temp.*-所有列
如果(温度值,就这样做:
select temp.* , --all your columns
if(temp.value<0,0,temp.value) as Value
from
( your query mentioned above) temp;
选择temp.*-所有列
如果(温度值,就这样做:
select temp.* , --all your columns
if(temp.value<0,0,temp.value) as Value
from
( your query mentioned above) temp;
选择temp.*-所有列
如果(温度值,就这样做:
select temp.* , --all your columns
if(temp.value<0,0,temp.value) as Value
from
( your query mentioned above) temp;
选择temp.*-所有列
如果(温度值最简单的方法就是使用grest()
:
选择最大(,0)作为列
不清楚您希望在查询中为哪个列/表达式执行此操作。但是,您可以将其插入
与其他方法相比,该方法的优点是:
- 只需在查询中出现一次。不必重复代码,可降低出错的可能性
- 不需要子查询。这在MySQL中是一个问题,因为MySQL实现了子查询
最简单的方法就是简单地使用grest()
:
选择最大(,0)作为列
不清楚您希望在查询中为哪个列/表达式执行此操作。但是,您可以将其插入
与其他方法相比,该方法的优点是:
- 只需在查询中出现一次。不必重复代码,可降低出错的可能性
- 不需要子查询。这在MySQL中是一个问题,因为MySQL实现了子查询
最简单的方法就是简单地使用grest()
:
选择最大(,0)作为列
不清楚您希望在查询中为哪个列/表达式执行此操作。但是,您可以将其插入
与其他方法相比,该方法的优点是:
- 只需在查询中出现一次。不必重复代码,可降低出错的可能性
- 不需要子查询。这在MySQL中是一个问题,因为MySQL是materiali