分组的地方不同于?

时间:2012-02-08 09:12:31

标签: mysql

我正在尝试创建一个查询,该查询将对按字段分组的结果BY进行分组。 我的查询是这样的:

问题是created_by_alias为空的所有行都被分组为一个,这是不好的。我只需要在值与''不同的地方进行分组。

由于某些原因,我无法将字段更改为NULL(网站在joomla上,为了做到这一点,我必须更改核心)。

SELECT c.created_by_alias, c.introtext,c.id,c.publish_up,c.publish_down 
FROM jos_content as c 
GROUP BY c.created_by_alias

这样的事情可能吗?

感谢您的时间和知识:)

示例:

|名字| groupMe |

---------------------------------------
| aaa       |           XX             |
---------------------------------------
| aaa       |           XX             |
---------------------------------------
| ccc       |                          |
--------------------------------------- 
| ddd       |                          |
---------------------------------------

I would like to receive such results:
---------------------------------------
| aaa       |           XX             |
---------------------------------------
| ccc       |                          |
--------------------------------------- 
| ddd       |                          |
---------------------------------------

I hope 

3 个答案:

答案 0 :(得分:4)

  SELECT c.created_by_alias, ...   
  FROM jos_content as c 
  WHERE c.created_by_alias <> ''
  GROUP BY c.created_by_alias
UNION ALL
  SELECT c.created_by_alias, ...   
  FROM jos_content as c 
  WHERE c.created_by_alias = ''

另一种方式是:

  SELECT c.created_by_alias, ...   
  FROM jos_content as c 
  GROUP BY c.created_by_alias
         , CASE WHEN c.created_by_alias = '' 
                  THEN c.PK                  --- the Primary Key
                  ELSE NULL
           END

答案 1 :(得分:4)

怎么样?

select name, count(*) from t1
group by name, if (name = '', id, -1)

答案 2 :(得分:3)

我可能会忽略你问题中的某些内容,但这不仅仅是一个WHERE - 条件,类似......

SELECT c.created_by_alias, c.introtext,c.id,c.publish_up,c.publish_down 
FROM jos_content as c
WHERE c.created_by_alias != "" 
GROUP BY c.created_by_alias

...

如果我错了,请提供一些示例数据,以便更清楚地了解您要归档的内容。