2016-11-21 64 views
0

我想顯示在SELECT中定義的所需列,一旦執行了一個條件並打印條件的結果,我想從SELECT中的新指定列中顯示另一個條件條款等等,首先不知何故,我需要解決這兩個查詢,之後,該計劃是混合約10個queires。感謝詩的是,i_di1.naziv如何加入來自多個SQL查詢的結果

SELECT bolnickiracun.id, 
     bolnickiracun.id_dijagnoza1, i_di1.naziv naziv_dijagnoza1 
FROM  bolnickiracun 
LEFT JOIN i_dijagnoze_marko i_di1 ON bolnickiracun.id_dijagnoza1=i_di1.id 
WHERE bolnickiracun.id_dijagnoza1!="" and i_di1.naziv is null 
UNION ALL 
SELECT bolnickiracun.id, 
     bolnickiracun.id_dijagnoza2, i_di2.naziv naziv_dijagnoza2 
FROM  bolnickiracun 
LEFT JOIN i_dijagnoze_marko i_di2 ON bolnickiracun.id_dijagnoza2=i_di2.id 
WHERE bolnickiracun.id_dijagnoza2!="" and i_di2.naziv is null 

我想結果是這樣的結果,一切都顯示在列的問題之一:

id id_dijagnoza1 naziv_dijagnoza1 id_dijagnoza2 naziv_dijagnoza2 
1  some data   null    
2  some data   null 
3          some data   null 
4          some data   null 

點是要找到naziv_dijagnoza1或naziv_dijagnoza2哪裏是空即,外鍵顯示不存在主鍵,或者某人導入不存在作爲主鍵的錯誤外鍵。

+0

SQL服務器IR MySQL的? – Jens

+0

@GordonLinoff sry,我的錯誤,是mysql – MPetrovic

+0

顯示'bolnickiracun','i_dijagnoze_marko'表結構,幾行數據和預期結果 –

回答

0

這個例子對於3 SELECT(第三個通過基於可以看到的模式的推論預測)應該有助於理解對於整個查詢需要做什麼10 SELECT's。您需要在每個SELECT之後的每個列中填寫NULL的SELECT子句。

SELECT bolnickiracun.id, bolnickiracun.id_dijagnoza1, i_di1.naziv naziv_dijagnoza1, NULL, NULL, NULL, NULL -- and so on 
FROM  bolnickiracun 
LEFT JOIN i_dijagnoze_marko i_di1 ON bolnickiracun.id_dijagnoza1=i_di1.id 
WHERE bolnickiracun.id_dijagnoza1!="" and i_di1.naziv is null 

UNION ALL 

SELECT bolnickiracun.id, NULL, NULL, bolnickiracun.id_dijagnoza2, i_di2.naziv naziv_dijagnoza2, NULL, NULL -- and so on 
FROM  bolnickiracun 
LEFT JOIN i_dijagnoze_marko i_di2 ON bolnickiracun.id_dijagnoza2=i_di2.id 
WHERE bolnickiracun.id_dijagnoza2!="" and i_di2.naziv is null 

UNION ALL 

SELECT bolnickiracun.id, NULL, NULL, NULL, NULL, bolnickiracun.id_dijagnoza3, i_di3.naziv naziv_dijagnoza3 -- and so on 
FROM  bolnickiracun 
LEFT JOIN i_dijagnoze_marko i_di3 ON bolnickiracun.id_dijagnoza3=i_di3.id 
WHERE bolnickiracun.id_dijagnoza3!="" and i_di3.naziv is null 

列的每SELECT數應是相同的,因爲你可以看到,對於3 SELECTš它的7列(每2表接合×3個SELECT S + 1與ID第一列)。對於每個SELECT條款中的每個012條中的21列(1代表id,2代表每個表中,其餘的填充NULL s)。 不是漂亮的解決方案,但可能只有像這樣的數據庫結構。

所以對於10 SELECT的弗里斯特最後應該是這樣的:

SELECT bolnickiracun.id, bolnickiracun.id_dijagnoza1, i_di1.naziv naziv_dijagnoza1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL 
FROM  bolnickiracun 
LEFT JOIN i_dijagnoze_marko i_di1 ON bolnickiracun.id_dijagnoza1=i_di1.id 
WHERE bolnickiracun.id_dijagnoza1!="" and i_di1.naziv is null 

UNION ALL 
... 
UNION ALL 

SELECT bolnickiracun.id, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, bolnickiracun.id_dijagnoza10, i_di10.naziv naziv_dijagnoza10 
FROM  bolnickiracun 
LEFT JOIN i_dijagnoze_marko i_di10 ON bolnickiracun.id_dijagnoza10=i_di10.id 
WHERE bolnickiracun.id_dijagnoza10!="" and i_di10.naziv is null 
+0

謝謝你,這工作正常,是否有可能,在第一次選擇,把所有名稱,並與其他選擇填充數據? – MPetrovic

+0

而不是。首先'SELECT'只加入'bolnickiracun'和'i_dijagnoze_marko'表,所以如果你嘗試選擇dijagnoza3 - 查詢將失敗。 我試着找到任何其他解決方案,例如somrthing與一個選擇和許多聯接,但我找不到任何 –

+0

我將解決與別名的列的名稱,這將是一個「體力工作」,因爲有很多手動調整,但沒關係,因爲我得到了我期望的結果。 – MPetrovic