SQLite查询中的条件分组

时间:2014-03-12 22:58:44

标签: android sqlite select

我有ORDER BY子句的查询,它运行良好但现在我希望如果field3等于字符串good,则不应对此字符串进行分组。我希望我解释一下。

String sql = "SELECT field1, field2, field3, field4, field5, COUNT (field3) 
FROM Table
WHERE field2 = '"+numero+"'
GROUP BY field3 ORDER BY field1 ASC ";

2 个答案:

答案 0 :(得分:1)

试试这个:

SELECT field1, field2, field3, field4, field5, COUNT (field3)
FROM MyTable
WHERE field2 = '5' AND field3 <> 'good'
GROUP BY field3

UNION ALL

SELECT field1, field2, field3, field4, field5, field3
FROM MyTable
WHERE field2 = '5' AND field3 = 'good'

ORDER BY field1

它使用union语句附加“好”字样。到最后。

答案 1 :(得分:-1)

如果您想从结果中排除filed3,请执行以下操作:

String sql = "SELECT * FROM (SELECT field1, field2, field3, field4, field5, COUNT (field3) 
FROM Table 
WHERE field2 = '"+numero+"'
GROUP BY field3 ORDER BY field1 ASC ) WHERE feild3 != 'good' ";
祝你好运