2012-05-03 64 views
0

下面的查詢適用於返回空列名稱,我試圖完成的工作是使其返回到第一個具有空值的列名稱。我還有另一個名爲電子郵件的專欄,每行都有一個唯一的電子郵件地址。我的問題是我無法弄清楚這樣做的適當語法。任何幫助表示讚賞!如何在特定行中找到具有空值的第一列名稱

我也爲此使用php5.2。

SELECT col FROM (SELECT 'GAME0' AS col, GAME0 AS value FROM USERNAME 
     UNION ALL SELECT 'GAME1', GAME1 FROM USERNAME 
     UNION ALL SELECT 'GAME2', GAME2 FROM USERNAME 
     UNION ALL SELECT 'GAME3', GAME3 FROM USERNAME 
     UNION ALL SELECT 'GAME4', GAME4 FROM USERNAME 
     UNION ALL SELECT 'GAME5', GAME5 FROM USERNAME 
     UNION ALL SELECT 'GAME6', GAME6 FROM USERNAME 
     UNION ALL SELECT 'GAME7', GAME7 FROM USERNAME 
     UNION ALL SELECT 'GAME8', GAME8 FROM USERNAME 
     UNION ALL SELECT 'GAME9', GAME9 FROM USERNAME 
     UNION ALL SELECT 'GAME10', GAME10 FROM USERNAME 
     UNION ALL SELECT 'GAME11', GAME11 FROM USERNAME 
     UNION ALL SELECT 'GAME12', GAME12 FROM USERNAME 
     UNION ALL SELECT 'GAME13', GAME13 FROM USERNAME 
     UNION ALL SELECT 'GAME14', GAME14 FROM USERNAME 
     UNION ALL SELECT 'GAME15', GAME15 FROM USERNAME 
     UNION ALL SELECT 'GAME16', GAME16 FROM USERNAME 
     UNION ALL SELECT 'GAME17', GAME17 FROM USERNAME 
     UNION ALL SELECT 'GAME18', GAME18 FROM USERNAME 
     UNION ALL SELECT 'GAME19', GAME19 FROM USERNAME 
    ) allValues 
    WHERE value is null and email='emailaddress' 

編輯: 如果上面的查詢是不需要的,我需要像下面,在那裏我可以改變第一個空值的值。語法再次是我最大的問題。

update username 
SET CASE GAME0 
    WHEN GAME0 IS NOT NULL THEN GAME1 
    WHEN GAME1 IS NOT NULL THEN GAME2 
    WHEN GAME2 IS NOT NULL THEN GAME3 
    WHEN GAME3 IS NOT NULL THEN GAME4 
    WHEN GAME4 IS NOT NULL THEN GAME5 
    WHEN GAME5 IS NOT NULL THEN GAME6 
    WHEN GAME7 IS NOT NULL THEN GAME8 
    WHEN GAME8 IS NOT NULL THEN GAME9 
    WHEN GAME9 IS NOT NULL THEN GAME10 
    WHEN GAME10 IS NOT NULL THEN GAME11 
    WHEN GAME11 IS NOT NULL THEN GAME12 
    WHEN GAME12 IS NOT NULL THEN GAME13 
    WHEN GAME13 IS NOT NULL THEN GAME14 
    WHEN GAME14 IS NOT NULL THEN GAME15 
    WHEN GAME15 IS NOT NULL THEN GAME16 
    WHEN GAME16 IS NOT NULL THEN GAME17 
    WHEN GAME17 IS NOT NULL THEN GAME18 
    WHEN GAME18 IS NOT NULL THEN GAME19 
    END where EMAIL='emailaddress' 
+1

此查詢對我沒有任何意義。爲什麼你連續獲取USERNAME表20次? –

+0

我誠實地不知道,這是我能夠得到它返回我需要的值的唯一方法,如果有更簡單的方式,請告訴我 – IceSteve

+0

'ORDER BY col LIMIT 1'? –

回答

3

你可以讓這個查詢多simplier和這樣做我通過PHP的計算:

$arr_field = array(); 
foreach(range(0, 19) as $i) 
    $arr_field[] = "GAME{$i}"; 

$str_add = implode(' + ', $arr_field); 
$str_select = implode(', ', $arr_field); 

$query = 
mysql_query(" 
    SELECT {$str_select} 
    FROM USERNAME 
    WHERE ({$str_add}) IS NULL AND email = 'emailaddress' 
"); 

// Then get the NULL column through a loop 

while($row = mysql_fetch_assoc($query)) 
    foreach($arr_field as $h) 
     if (! isset($row[$h])) { 

      echo "Found column! {$h}<br />"; 
      break; 

     } 

Baiscly我添加的所有20場在一起,如果這些20個數值是NULL,結果變成NULL。我獲取所有20個值,然後通過PHP循環找到空字段。但說實話,我已經做出了另一個結構來處理這些GAME0 - GAME19字段,而不是使用第二個表格來處理這些GAME0 - GAME19字段。

+0

非常感謝,並感謝您的建議。 – IceSteve

1

我真的不明白你的表結構,但這裏有一個簡單的方法:只需添加order by col limit 1到您的查詢。

編輯:這個工作,你將不得不使用"GAME00", "GAME01"等。(你不必改變列名,只是你在字符串中放置名)

編輯2:將其包含在你的子查詢中存在的電子郵件地址,像這樣:

SELECT col FROM (SELECT 'GAME00' AS col, GAME0 AS value, email FROM USERNAME 
    UNION ALL SELECT 'GAME01', GAME1, email FROM USERNAME 

(ETC)

+0

感謝您的回答,我相信解決了第一個價值問題,因爲我已經選擇了所有遊戲列,它表示沒有電子郵件字段來搜索我輸入的電子郵件地址,如何將它包含在查詢 – IceSteve

相關問題