2016-09-29 57 views
-2

我有兩個autocomplettextview和3列在我的表col1,col2,col3。 COL1是主鍵SQLite:如何搜索多個值?

表是這樣的:

| col1 | col2 | col3     | 
----------------------------------------- 
| 1 | store1 |apple,pineapple,mango | 
| 2 | store2 | apple,orange   | 
| 3 | store3 | apple,pineapple,orange| 
| 4 | store4 | orange,mango   | 
| 5 | store5 | mango,jackfruit  | 

我有兩個問題:

Q1:我可以將多個值存儲在單一column.If沒有那麼我該怎麼存放?

Q2:以前說我有兩個autocompletetextview搜索

即:

如果用戶鍵入的蘋果和2 autocompletetextview菠蘿那麼我就需要 顯示行,這兩個文本相匹配。

預期的O/P

| col1 | col2 | col3     | 
----------------------------------------- 
| 1 | store1 |apple,pineapple,mango | 
| 2 | store3 | apple,pineapple,orange| 

我怎麼能做到這一點?

我通過幾個reseaches走了,帶着的foreign key的幫助下,我可以存儲在單個列多個值,但如何使用Where clause外鍵實現expected o/p

+1

請學習一些基本的SQL ...您可以連接的表+ https://en.wikipedia.org/wiki/Database_normalization – Selvin

+1

您第一個問題你應該創建兩個表,並簡單地加入他們! –

+0

如果您有2個問題,請在單獨的帖子中提問。 – Shadow

回答

1

你不應該在同一列中存儲多個值。相反,你應該使用三張表。

那麼容易找到什麼STOREID兼得食品

SELECT FoodAvailable.StoreID 
FROM FoodAvailable 
JOIN Foods 
    ON FoodAvailable.FoodID = Foods.FoodID 
WHERE Foods.Food in ('apple', 'pineapple') 
GROUP BY FoodAvailable.StoreID 
HAVING count(*) = 2 

商店

| StoreID | StoreName | 
----------------------- 
| 1  | store1 | 
| 2  | store2 | 
| 3  | store3 | 
| 4  | store4 | 
| 5  | store5 | 

食品

| FoodID | Food  | 
----------------------- 
| 1  | apple  | 
| 2  | pineapple | 
| 3  | mango  | 
| 4  | orange | 
| 5  | jackfruit | 

FoodAvailable

| StoreID | FoodID | 
--------------------- 
| 1  | 1  | //apple 
| 1  | 2  | //pineapple 
| 1  | 3  | //mango 
| 2  | 1  | //apple 
| 2  | 4  | //orange 
| 3  | 1  | //apple 
| 3  | 2  | //pineapple 
| 3  | 4  | //orange 
| 4  | 4  | //orange 
| 4  | 3  | //mango 
| 5  | 3  | //mango 
| 5  | 5  | //jackfruit 

你最終的查詢是這樣的

SELECT f.StoreID, GROUP_CONCAT(food ORDER BY food DESC SEPARATOR ', ') 
FROM FoodAvailable f 
WHERE f.StoreID = (SELECT FoodAvailable.StoreID 
        FROM FoodAvailable 
        JOIN Foods 
        ON FoodAvailable.FoodID = Foods.FoodID 
        WHERE Foods.Food in ('apple', 'pineapple') 
        GROUP BY FoodAvailable.StoreID 
        HAVING count(*) = 2 
       ) 
GROUP BY f.StoreID 
+0

感謝您的迴應..我需要根據用戶的搜索顯示,然後我們如何使用IN運算符@Juan – madhushri

+0

檢查更新.... –

+0

用戶可以搜索autocompletextview中的任何內容,然後如何在IN運算符中輸入apple,pineapple。用戶還可以鍵入橙色,蘋果或其他一些組合@Juan – madhushri