有条件的分组子句(sqlite) [英] Conditional grouping clause (sqlite)
本文介绍了有条件的分组子句(sqlite)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
SELECT 语句中,如果 col_B = GROUP BY col_A
some_value
,否则 GROUP BY col_C
?然后让那个用于分组结果的字段? 像
SELECT value_of_the_column_used_for_group
FROM table WHERE ...
GROUP BY(如果col_B =some_value:col_A:ELSE:col_C)
LIMIT 0,20;
基本上我想要选择具有不同父母或具有不同ID的父记录的子记录。但是我想选择父ID,它可以是ID列或儿童记录的parent_ID列。
你,你可以使用 case
语句: SELECT(col_B = 'some_value'then col_A ELSE col_C end)
FROM table WHERE ...
GROUP BY(col_B ='some_value'时col_A ELSE col_C结束时)
LIMIT 0,20;
Inside a SELECT
statement, is it possible to GROUP BY col_A
if col_B = "some_value"
, otherwise GROUP BY col_C
? Then have that field that was used for grouping inside the results?
Like
SELECT value_of_the_column_used_for_group
FROM table WHERE ...
GROUP BY (IF col_B = "some_value" : col_A : ELSE : col_C)
LIMIT 0, 20;
Basically I want to select children records that have distinct parents or parent records that have distinct IDs. But I want to select parent IDs, which can be the ID column or the "parent_ID" column from children records
解决方案
You, you can use a case
statement:
SELECT (case when col_B = 'some_value' then col_A ELSE col_C end)
FROM table WHERE ...
GROUP BY (case when col_B = 'some_value' then col_A ELSE col_C end)
LIMIT 0, 20;
这篇关于有条件的分组子句(sqlite)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文