MySQL在连接表中使用IF或CASE语句

时间:2014-05-02 09:53:16

标签: mysql sql if-statement case

所有这里都是一个MySQL问题,它使用来自2表连接的结果,有条件地评估它们并输出2个值。

这是数据库结构。

第一个表格gtpro包含

用户ID(列名称ID)

样本/年份数,即2年,4年或12年/年(列名labSamples__yr)

第二桌实验结果包含

相同的用户ID(列名idgtpro)

和样本日期的日期列(提供样本时)列名称日期

因此,此查询会返回所有ID的概述以及为该ID提交的最后一个样本的时间。

SELECT a.id, a.labSamples__yr, max(b.date) as ndate from gtpro as a 
join labresults as b on a.id = b.idgtpro group by a.id

我要评估的条件是这样的。

a.labSamples__yr = 2 and ndate >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
a.labSamples__yr = 4 and ndate >= DATE_SUB(CURDATE(), INTERVAL 3 MONTH)
a.labSamples__yr = 12 and ndate >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)

因此,如果样本数/年为2且最后一个samle日期超过6个月,我想知道该id的样本的id和最新日期。

我尝试过使用CASE和IF语句,但不能正确使用它。这是我最近的尝试。

select id, ndate,
case when (labSamples__yr = 2 and ndate <= DATE_SUB(CURDATE(), INTERVAL 6 MONTH))is true
then
(SELECT id from gtpro as a join labresults as b on a.id = b.idgtpro where
labSamples__yr = 2 and max(b.date) <= DATE_SUB(CURDATE(), INTERVAL 6 MONTH)) end as id
from (SELECT a.id, a.labSamples__yr, max(b.date) as ndate from gtpro as a 
join labresults as b on a.id = b.idgtpro group by a.id) d

这告诉我无效使用群组功能。

急需一点帮助

编辑我搞砸了上面代码中的一些名字,我现在修复了这些名字。

3 个答案:

答案 0 :(得分:0)

这是否意味着有效的MySQL?我不知道&#34;是真的&#34;在CASE声明中有效。公平地说,虽然我对Oracle和SQL Server比较熟悉,但是......这个陈述的任何部分都有用吗?

修改

好的,这是我编辑代码的原因:

select id, ndate,
case when (labSamples__yr = 2 and ndate <= DATE_SUB(CURDATE(), INTERVAL 6 MONTH))
then
(SELECT id from bifipro as a join labresults as b on a.id = b.idBifipro where
labSamples__yr = 2 and max(b.date) <= DATE_SUB(CURDATE(), INTERVAL 6 MONTH) where     a.id=d.id) end as id
from (SELECT a.id, a.labSamples__yr, max(b.date) as ndate from bifipro as a 
join labresults as b on a.id = b.idBifipro group by a.id) d
  • 在您的相关子查询中,我添加了 &#34;的谓词,其中a.id = d.id&#34;
  • 我已从您的个案陈述中删除 &#34;是真的&#34; 不正确,但我没有&#39;认为应该在那里。

答案 1 :(得分:0)

如果我理解你的问题,你应该能够把条件放在where条款中:

SELECT a.id, a.labSamples__yr, max(b.date) as ndate
from gtpro a join
     labresults b
     on a.id = b.idgtpro
where (a.labSamples__yr = 2 and b.date >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH)) or
      (a.labSamples__yr = 4 and b.date >= DATE_SUB(CURDATE(), INTERVAL 3 MONTH)) or
      (a.labSamples__yr = 12 and b.date >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH))
group by a.id;

这可以解决您的语法问题。但是,如果您希望id具有最大日期,请尝试执行以下操作:

select a.labSamples__yr, max(b.date) as ndate,
       substring_index(group_concat(a.id order by b.date desc)) as maxid
from gtpro a join
     labresults b
     on a.id = b.idgtpro
where (a.labSamples__yr = 2 and b.date >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH)) or
      (a.labSamples__yr = 4 and b.date >= DATE_SUB(CURDATE(), INTERVAL 3 MONTH)) or
      (a.labSamples__yr = 12 and b.date >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH))
group by a.labSamples__yr;

a.id放在group by中并不会为您提供任何内容的最大ID。

答案 2 :(得分:0)

答案部分受到Tomas的启发(sql澄清和语法澄清)我一起摆脱了CASE。这对我来说似乎很好,但我想听听任何其他建议

select id, labSamples__yr, ndate from
(SELECT a.id, a.labSamples__yr, max(b.date) as ndate from gtpro as a 
join labresults as b on a.id = b.idgtpro  group by a.id)d 
where (ndate <= DATE_SUB(CURDATE(), INTERVAL 6 MONTH) and labSamples__yr = 2) 
or (ndate <= DATE_SUB(CURDATE(), INTERVAL 3 MONTH) and labSamples__yr = 4)
or (ndate <= DATE_SUB(CURDATE(), INTERVAL 1 MONTH) and labSamples__yr = 12)

感谢您的期待,但是看到使用CASE语句的解决方案以供日后参考仍然很高兴