在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的所有值。其原因是 distinct只能返回它的目标字段,而无法返回其它字段,这个问题让我困扰了很久,用distinct不能解决的话,我只有用二重循环查询来解决,而 这样对于一个数据量非常大的站来说,无疑是会直接影响到效率的。
1.返回不重复记录的条数
<select id="getSentMessagesCount" parameterType="map" resultType="Integer">
<![CDATA[
SELECT
COUNT(DISTINCT result.created_time)
FROM
(SELECT *
FROM message
UNION ALL
SELECT *
FROM read_message
) result
INNER JOIN class_section cs
ON
result.entity_id = cs.id
AND cs.is_deleted = 0
WHERE
result.entity_type = 'customMessage'
AND result.from_user_id = #{userId}
AND result.entity_id = #{classId}
]]>
</select>
2. 返回不重复记录
SELECT *, COUNT(DISTINCT name) FROM table GROUP BY name
注意:如果有ORDER BY和LIMIT,GROUP BY应该放在他们之前。
下面是一个生产实例,一个老师可以对班上的学生群发消息,现在是要根据老师的userId和class_id找到
用户所发送过的消息:
<select id="findSentMessages" parameterType="map" mMap="messageDTOMap">
<![CDATA[
SELECT
m.from_user_id AS from_user_id,
m.title AS title,
m.body AS body,
m.entity_id AS entity_id,
m.entity_type AS entity_type,
m.created_time AS created_time,
m.updated_time AS updated_time,
COUNT(DISTINCT m.created_time) AS count
FROM message m
INNER JOIN class_section cs
ON
m.entity_id = cs.id
AND cs.is_deleted = 0
WHERE
m.entity_type = 'customMessage'
AND m.from_user_id = #{userId}
AND m.entity_id = #{classId}
GROUP BY
m.created_time ASC
]]>
</select>
其中count字段是多余的,不用管;