我有一个表,一个对象有几行。我正在尝试对行进行分组,以便它们根据链接列形成一个列。我有一些SQL可以做到这一点,但现在我不知道如何使它基于另一个字段的条件。
******************************************************
* id * page_id * key * value * uuid *
******************************************************
* 1 * 1 * type * url * 654978654 *
******************************************************
* 2 * 1 * picture * 654978654.png * 654978654 *
******************************************************
* 3 * 1 * url * google.com * 654978654 *
******************************************************
* 4 * 1 * active * 1 * 654978654 *
******************************************************
所以这基本上只是表格中的一个条目。如果每个uuid的active都设置为1,我希望能够让我的select只能工作。下面是我用来将行设置为列的SQL(这确实有用)。
Select uuid
, Min( Case When meta_key = "type" Then meta_value End ) As Type
, Min( Case When meta_key = "picture" Then meta_value End ) As Picture
, Min( Case When meta_key = "url" Then meta_value End ) As URL
, Min( Case When meta_key = "active" Then meta_value End ) As Active
From sm_page_meta
Where meta_key In("type","picture","url","active")
Group By uuid
答案 0 :(得分:2)
只需添加having
子句:
Select uuid
, Min( Case When meta_key = 'type' Then meta_value End ) As Type
, Min( Case When meta_key = 'picture' Then meta_value End ) As Picture
, Min( Case When meta_key = 'url' Then meta_value End ) As URL
, Min( Case When meta_key = 'active' Then meta_value End ) As Active
From sm_page_meta
Where meta_key In('type', 'picture', 'url', 'active')
Group By uuid
Having Active = '1';
如果您不想在结果集中包含Active
,只需将整个表达式放在HAVING
子句中。