2014-02-22 56 views
0

通過Android/java在Sqlite中生成交叉表有什麼更好的選擇?Android sqlite Crosstab

我:

[TABLE people] 
<i>_id, NAME 
1, "mary" 
2, "juan" 
3, "jose"</i> 

[TABLE GLASSES] 
_id, COLOR 
1, "BLACK" 
2, "BLUE" 
3, "GRAY" 
4, "YELLOW" 
... 

[TABLE PEOPLE_GLASSES] 
_id, idpeople, idglass, qty 
1, 1, 1, 50 
2, 1, 3, 30 
3, 1, 4, 25 
... 

我需要:

[crosstab] 
NAME | BLACK | GRAY | YELLOW 
"mary" | 50 | 30 | 25 
... 

如何做到這一點?

+2

清除你的問題的意圖... –

+0

正是你想要的.. ???我想你想使用加入表,是嗎? –

+0

No.連接不會將行轉換爲列 – AnselmoMS

回答

0

SQLite沒有任何內置函數來將行轉換爲列。

你必須先讀GLASSES表,然後,基於該數據,動態創建另一個查詢是這樣的:

SELECT NAME, 
     (SELECT qty 
     FROM PEOPLE_GLASSES 
     WHERE idpeople = people._id 
      AND idglass = 1 
     ) AS "BLACK", 
     (SELECT qty 
     FROM PEOPLE_GLASSES 
     WHERE idpeople = people._id 
      AND idglass = 2 
     ) AS "BLUE", 
     (SELECT qty 
     FROM PEOPLE_GLASSES 
     WHERE idpeople = people._id 
      AND idglass = 3 
     ) AS "GRAY", 
     (SELECT qty 
     FROM PEOPLE_GLASSES 
     WHERE idpeople = people._id 
      AND idglass = 4 
     ) AS "YELLOW" 
FROM people 
+0

這不是非常有效。如果我的桌子增長? – AnselmoMS

+0

你會如何更有效地運作? –