MySQL函数IF给出了错误

时间:2014-05-27 09:07:17

标签: mysql

CREATE TABLE 111_02DetermineCdeApproachable
SELECT view_0501_011_01RecipientEgroup.recipient_id,
SELECT Last(group_id) FROM view_0501_011_01RecipientEgroup AS group_id,
if(group_id = 60 or group_id = 52 or group_id = 49 or group_id = 40 or group_id = 30,2,1) AS cde_approachable FROM tbl_111_01SelectMaxCreateDate
INNER JOIN view_0501_011_01RecipientEgroup
ON (tbl_111_01SelectMaxCreateDate.recipient_id = view_0501_011_01RecipientEgroup.recipient_id)
AND (tbl_111_01SelectMaxCreateDate.max_create_date = view_0501_011_01RecipientEgroup.create_date)
GROUP BY view_0501_011_01RecipientEgroup.recipient_id;

查询错误!

  

CREATE TABLE 111_02DetermineCdeApproachable SELECT view_0501_011_01RecipientEgroup.recipient_id, SELECT Last(group_id) FROM view_0501_011_01RecipientEgroup AS group_id, if(group_id = 60 or group_id = 52 or group_id = 49 or group_id = 40 or group_id = 30,2,1) AS cde_approachable FROM tbl_111_01SelectMaxCreateDate INNER JOIN view_0501_011_01RecipientEgroup ON (tbl_111_01SelectMaxCreateDate.recipient_id = view_0501_011_01RecipientEgroup.recipient_id) AND (tbl_111_01SelectMaxCreateDate.max_create_date = view_0501_011_01RecipientEgroup.create_date) GROUP BY view_0501_011_01RecipientEgroup.recipient_id

     

你的错误   SQL语法;查看与MySQL服务器对应的手册   在第3行'SELECT Last(group_id) FROM view_0501_011_01RecipientEgroup AS group_id, if(grou'附近使用正确语法的版本

2 个答案:

答案 0 :(得分:0)

尝试使用此代码: -

if(group_id in (60, 49, 40) or groep_id in (52, 30, 2, 1) AS cde_approachable

如果group_id和groep_id不是你的错误。但如果它们相同,那么你必须尝试这样的事情: -

if(group_id in (60, 49, 40, 52, 30, 2, 1) AS cde_approachable

希望这可以帮到你。

答案 1 :(得分:0)

试试这个

CREATE TABLE 111_02DetermineCdeApproachable 
  SELECT v.recipient_id
       , Last( v.group_id )
       , if( group_id in ( 60, 52, 49, 40, 30 ), 2, 1 ) AS cde_approachable
  FROM tbl_111_01SelectMaxCreateDate t
       INNER JOIN view_0501_011_01RecipientEgroup v
               ON ( t.recipient_id    = v.recipient_id ) 
              AND ( t.max_create_date = v.create_date )
  GROUP BY v.recipient_id;