2014-03-28 33 views
0

我想從使用slim框架的mysql數據庫檢索特定的一組數據。我的sql查詢不會讓我嚴格想要什麼。任何幫助將非常感激!沒有從SQL返回想要的結果

查詢:

function getModules($matric) 
    { 
    $sql = "SELECT DISTINCT students.module1,time_tables.days, time_tables.time FROM `students`,`time_tables` 
    WHERE students.matricNos = '$matric' AND students.module1 = time_tables.module_tag 

    UNION 

    SELECT DISTINCT students.module2,time_tables.days, time_tables.time FROM `students`,`time_tables` 
    WHERE students.matricNos = '$matric' AND students.module2 = time_tables.module_tag 

    UNION 

    SELECT DISTINCT students.module3,time_tables.days, time_tables.time FROM `students`,`time_tables` 
    WHERE students.matricNos = '$matric' AND students.module3 = time_tables.module_tag"; 


    try 
    { 
     $db = getConnection(); 
     $stmt = $db-> query($sql); 
     $result = $stmt-> fetchAll(PDO::FETCH_OBJ); 
     $db=null; 
     echo json_encode($result); 
    } 
    catch(PDOException $e) { 
     echo '{"error":{"text":'. $e->getMessage() .'}}'; 
    } 

} 

什麼,我從查詢找回

[{"module1":"ACC07103","days":"Thursday","time":"09:00-10:00"},{"module1":"CLP07112","days":"Tuesday","time":"14:00-15:00"},{"module1":"BMS08100","days":"Thursday","time":"10:00-11:00"}] 

什麼在這裏發生的是它給了我正確的價值觀,但它給我像模塊1相同的標籤,當它顯示了另一個模塊,它應該是module2,但它顯示模塊1以及模塊3相同。

我想要的結果;

[{"module1":"ACC07103","days":"Thursday","time":"09:00-10:00"},{"module2":"CLP07112","days":"Tuesday","time":"14:00-15:00"},{"module3":"BMS08100","days":"Thursday","time":"10:00-11:00"}] 
我的數據庫

列標記像模塊1,模塊2,單詞數,但我不明白爲什麼它給我正確的價值觀,但相同的名稱爲不同的列..

謝謝..

回答

1

我懷疑這是關於從第一個SELECT查詢中取出列名的UNION。

如果將SELECT DISTINCT students.module1更改爲SELECT DISTINCT students.module1 as module,這仍然會發生嗎?

+0

只是爲了詳細說明這一點。我懷疑你能夠得到你想要的結果,因爲改變JSON輸出中的關鍵值會適得其反。您是否可以添加一個新的鍵值對,您只需在SELECT查詢中填充一個字符串,例如'SELECT DISTINCT'module3'as newKey,students.module3 as module;' –

0

嘗試:

$sql = "SELECT students.module1,null as module2,null as module3, time_tables.days, time_tables.time FROM `students`,`time_tables` 
    WHERE students.matricNos = '$matric' AND students.module1 = time_tables.module_tag 
    GROUP BY students.module1 

    UNION 

    SELECT null as module1,students.module2,null as module3, time_tables.days, time_tables.time FROM `students`,`time_tables` 
    WHERE students.matricNos = '$matric' AND students.module2 = time_tables.module_tag 
    GROUP BY students.module2 
    UNION 

    SELECT null as module1, null as module2,students.module3,time_tables.days, time_tables.time FROM `students`,`time_tables` 
    WHERE students.matricNos = '$matric' AND students.module3 = time_tables.module_tag 
    GROUP BY students.module3"; 
在這種形式給出

,你會選擇模塊1,模塊2,單詞數與空值,如果它不匹配。

+0

嗨,感謝這個工作,我確實爲我不想使用的模塊獲得空值。這樣我就可以忽略空值並專門獲取模塊*值。感謝您的快速回復。 – sponturious

+0

你歡迎:),你可以稍後忽略空值,OBS通過你喜歡的例子0或者''或者你喜歡的來替代NULL。我只是讓NULL。 –

0

除非你想運行3個查詢,否則沒有辦法做到你想要的。
您的列名無法更改中間查詢。

您可以運行3個查詢,而不是在SQL中使用UNION。

然後加入你的結果放在一起,是這樣的:

$result1 = $stmt1-> fetchAll(PDO::FETCH_OBJ); 
$result2 = $stmt2-> fetchAll(PDO::FETCH_OBJ); 
$result3 = $stmt3-> fetchAll(PDO::FETCH_OBJ); 

$result = array_merge($result1,$result2,$result3); 

或者,您也可以在查詢中添加一個額外的列指示模塊:

$sql = " 
SELECT DISTINCT 'module1' as module, students.module1,time_tables.days, time_tables.time 
    FROM `students`,`time_tables` 
    WHERE students.matricNos = '$matric' AND students.module1 = time_tables.module_tag 

UNION 

    SELECT DISTINCT 'module2',students.module2,time_tables.days, time_tables.time 
    FROM `students`,`time_tables` 
    WHERE students.matricNos = '$matric' AND students.module2 = time_tables.module_tag 

UNION 

    SELECT DISTINCT 'module3', students.module3,time_tables.days, time_tables.time 
    FROM `students`,`time_tables` 
    WHERE students.matricNos = '$matric' AND students.module3 = time_tables.module_tag"; 

然後,你將不得不構建你顯示或保存例程以從不同列中拉取模塊名稱,而不是使用列名稱本身。