2014-01-11 57 views
1

我試着顯示信息之前格式化我的所有數據在我的SQL增加的情況下statment到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 

回答

2

的情況下,應在SELECT列表,而不是INNER JOIN後。 第二個問題是在每個THEN語句之後使用l.status =。 MySql語法只需要THEN語句後的最終值,而不是值賦值。

還要注意的是,你不需要在你的when語句l.status =部分,因爲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 
+0

它返回一個0,如果這是因爲該列被歸類爲一個INT,並即時更改爲字符? – user934902

+0

@ user934902我認爲這個問題可能是每個THEN語句後的「l.status =」;只有那裏需要價值,試試我的第二種寫法,看看它是否有效。 –

-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 
+0

缺少'END AS狀態',否則該情況會作爲列標題返回 – user934902

+0

您是否測試了這個?如果您發佈的代碼沒有經過測試,您可能會收到降薪。 –

3
  1. 您希望您的CASESELECT條款
  2. 您的CASE語法無效
  3. 作爲交替ative你可以使用MySQL(因爲你的問題標有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 

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 

這裏是SQLFiddle演示(對於兩種情況)