2013-10-31 90 views
1

我有這個表:SQLiteDatabase需要幫助查詢......請

id | CUPNAME | FRENCHNAME 
1 | 2  | null 
2 | null | 4 

我想拉從CUPNAME和FRENCHNAME數據是不爲空。 該表可以填充有數字1至10在該示例中,應當能夠CUPNAME = 2,FRENCHNAME = 4

有這樣的查詢:

SELECT sum(ifnull(french_press_bd.CUPNAME,0)) as a , sum(ifnull(french_press_bd.FRENCHNAME,0)) as b FROM my_table WHERE id in (1,2) 

我嘗試寫所以這是行不通的:

String sqr = "id in (1,2)"; 
Cursor cursor = sqdb.query(french_press_bd.TABLE_NAME, new String[] {     
"sum(ifnull(french_press_bd.CUPNAME,0)) as a , sum(ifnull(french_press_bd.FRENCHNAME,0)) as b" 
},sqr, null,null,null,null); 
+0

你需要給兩個字符串(每列一個),但你是什麼真正想要的? –

+0

我想從表中引入一行數據,刪除值爲空 – comSaaxov

+0

請編輯您的問題以顯示輸入表和所需的輸出。 –

回答

1

我認爲你可以使用rawQuery。正如:

String your_query= "SELECT sum(ifnull(french_press_bd.CUPNAME,0)) as a , sum(ifnull(french_press_bd.FRENCHNAME,0)) as b FROM my_table WHERE id in (1,2)"; 
Cursor result = sqdb.rawQuery(your_query, null); 

但要確保SQLiteDatabaseNULL。您收到來電,

sqdb = dbhelper.getReadableDatabase(); // or getWritableDatabase() 
+0

哦謝謝提示,但顯然我沒有正確創建查詢。 我有一個表,兩列和TSUPNAME TSUPNAME和兩行值的第一行CUPNAME = 1 = FRENCHNAME null 值在第二行CUPNAME = null FRENCHNAME = 2 我想獲得日誌值在一個方式「12」並擺脫空 – comSaaxov

+1

@ user2923449你應該已經在OP中指定了這個要求! –

0

此查詢使用兩個標量子查詢來從各個欄的第一個非NULL值:

SELECT (SELECT CUPNAME 
     FROM my_table 
     WHERE id IN (1, 2) 
      AND CUPNAME IS NOT NULL 
     ) AS CUPNAME, 
     (SELECT FRENCHNAME 
     FROM my_table 
     WHERE id IN (1, 2) 
      AND FRENCHNAME IS NOT NULL 
     ) AS FRENCHNAME; 

由於外部查詢實際上並沒有做任何事情,它可能是簡單的只是單獨執行這兩個查詢:

long cName = db.compileStatement("SELECT CUPNAME FROM my_table" + 
           " WHERE id IN (1, 2) AND CUPNAME IS NOT NULL" 
           ).simpleQueryForLong(); 
long fName = db.compileStatement("SELECT FRENCHNAME FROM my_table" + 
           " WHERE id IN (1, 2) AND FRENCHNAME IS NOT NULL" 
           ).simpleQueryForLong(); 
+0

好的,你可以做一個查詢沒有引用ID? – comSaaxov

+0

我不明白你的問題。 –