将case语句添加到SQL连接语句

时间:2014-01-11 05:03:31

标签: mysql sql sql-server

我试图在显示信息之前格式化我的sql中的所有数据

我的联接都工作正常,我有一段数据(状态),它具有与值相关联的标题,但CASE语句不起作用。我目前有一个PHP swtich语句执行此数据转换但我想在SQL中尽可能

SELECT
 l.leadid, l.companyname, l.status, l.dateadded, addedby.name AS addedby, assignedto.name AS assignedto, l.address, l.city, l.prov, l.country, l.postalcode,
 l.phone, l.fax, l.facebook, l.twitter, l.youtube, l.instagram, l.website, l.logo
FROM leads l
  INNER JOIN staff addedby ON l.addedby = addedby.staffid
  INNER JOIN staff assignedto ON l.assignedto = assignedto.staffid
    CASE l.status
     WHEN l.status = 7 THEN l.status = 'Converted'
     WHEN l.status = 6 THEN l.status = 'Hot'
     WHEN l.status = 5 THEN l.status = 'Warm'
     WHEN l.status = 4 THEN l.status = 'Open'
     WHEN l.status = 3 THEN l.status = 'NEW'
     WHEN l.status = 2 THEN l.status = 'Cold'
       ELSE l.status = 'Dead'
     END    
     WHERE l.leadid = $id

3 个答案:

答案 0 :(得分:3)

  1. 您需要CASE SELECT条款中的CASE
  2. 您的mysql语法无效
  3. 作为替代方案,您可以使用MySQL(因为您的问题标记为ELT())特定的较不详细的函数SELECT l.leadid, l.companyname, CASE l.status WHEN 7 THEN 'Converted' WHEN 6 THEN 'Hot' WHEN 5 THEN 'Warm' WHEN 4 THEN 'Open' WHEN 3 THEN 'NEW' WHEN 2 THEN 'Cold' ELSE 'Dead' END status, l.dateadded, addedby.name AS addedby, assignedto.name AS assignedto, l.address, l.city, l.prov, l.country, l.postalcode, l.phone, l.fax, l.facebook, l.twitter, l.youtube, l.instagram, l.website, l.logo FROM leads l JOIN staff addedby ON l.addedby = addedby.staffid JOIN staff assignedto ON l.assignedto = assignedto.staffid WHERE l.leadid = $id
  4. 据说你可以做任何一件事

    SELECT
           l.leadid, l.companyname, 
           IFNULL(ELT(l.status - 1, 'Cold', 'New', 'Open', 'Warm', 'Hot', 'Converted'), 'Dead') status,
           l.dateadded, addedby.name AS addedby, 
           assignedto.name AS assignedto, l.address, 
           l.city, l.prov, l.country, l.postalcode,
           l.phone, l.fax, l.facebook, l.twitter, 
           l.youtube, l.instagram, l.website, l.logo
      FROM leads l JOIN staff addedby 
        ON l.addedby = addedby.staffid JOIN staff assignedto 
        ON l.assignedto = assignedto.staffid   
    WHERE l.leadid = $id
    

    {{1}}

    以下是 SQLFiddle 演示(针对这两种情况)

答案 1 :(得分:2)

案例应该在SELECT列表中,而不是INNER JOIN之后。 第二个问题是在每个THEN语句之后使用l.status =。 MySql语法只需要THEN语句之后的最终值,而不是值赋值。

另请注意,l.status =语句中不需要when部分,因为case value语句旁边列出了CASE。如果您正在构建临时案例陈述,那么将需要它。所以,您可以按如下方式编写它:

我只想写如下:

SELECT
    l.leadid, 
    l.companyname, 
    CASE l.status
        WHEN 7 THEN 'Converted'
        WHEN 6 THEN 'Hot'
        WHEN 5 THEN 'Warm'
        WHEN 4 THEN 'Open'
        WHEN 3 THEN 'NEW'
        WHEN 2 THEN 'Cold'
        ELSE 'Dead'
    END As Status, 
    l.dateadded, 
    < rest of your fields >
FROM leads l
    INNER JOIN staff addedby ON l.addedby = addedby.staffid
    INNER JOIN staff assignedto ON l.assignedto = assignedto.staffid
WHERE l.leadid = $id

答案 2 :(得分:-2)

试一试

SELECT
 l.leadid, l.companyname, l.status, l.dateadded, addedby.name AS addedby, assignedto.name AS assignedto, l.address, l.city, l.prov, l.country, l.postalcode,
 l.phone, l.fax, l.facebook, l.twitter, l.youtube, l.instagram, l.website, l.logo, CASE l.status
     WHEN l.status = 7 THEN l.status = 'Converted'
     WHEN l.status = 6 THEN l.status = 'Hot'
     WHEN l.status = 5 THEN l.status = 'Warm'
     WHEN l.status = 4 THEN l.status = 'Open'
     WHEN l.status = 3 THEN l.status = 'NEW'
     WHEN l.status = 2 THEN l.status = 'Cold'
       ELSE l.status = 'Dead'
     END
FROM leads l
  INNER JOIN staff addedby ON l.addedby = addedby.staffid
  INNER JOIN staff assignedto ON l.assignedto = assignedto.staffid    
WHERE l.leadid = $id