MySQL Query用于获取多个不同的值

时间:2012-12-19 18:36:46

标签: mysql select distinct

id  qid answer  date                    answer_userKey
72  2   2       2012-07-30 00:00:00     1
71  1   4       2012-07-30 00:00:00     1
70  2   2       2012-07-30 00:00:00     2
69  1   4       2012-07-30 00:00:00     2
68  2   2       2012-07-30 00:00:00     3
67  1   3       2012-07-30 00:00:00     3
66  2   2       2012-07-31 00:00:00     4
65  1   4       2012-07-31 00:00:00     4
64  2   2       2012-07-31 00:00:00     5

这是我的示例表,我需要为这样的每个日期获取所有数据+所有不同的answer_userKeys

date           DISTINCT(answer_userKey)
2012-07-30     3
2012-07-31     2

与普通关联数组中的所有单个值一样,就像你在

时得到的那样
SELECT * FROM tbl_data 
WHERE date BETWEEN '2012-07-29' AND '2012-08-01'

在这里尝试了一切:(

3 个答案:

答案 0 :(得分:1)

试试这个:

SELECT DATE(date) dte, COUNT(DISTINCT answer_userKey) cnt
FROM tbl_data 
WHERE DATE(date) BETWEEN '2012-07-29' AND '2012-08-01'
GROUP BY dte;

答案 1 :(得分:0)

我希望这是你的要求..这个在oracle中运行

select to_char(date,'yyyy-mm-dd') date
      ,count(distinct answer_userKey) DISTINCT(answer_userKey) 
from table_name 
group by to_char(date,'yyyy-mm-dd')

答案 2 :(得分:0)

如果您需要详细信息和聚合数据,那么您可能需要执行两个查询来获取这两个不同的数据集,或者只是查询详细信息并使用您正在使用的任何语言构建聚合(可能在一个多维数组)。例如,在伪代码中:

Query: SELECT * FROM tbl_data WHERE date BETWEEN '?' AND '?'

array; // multidimensional associative array to be populated
while ([fetch next row from result set as row] ) {
    array[row['date']][row['answer_userKey']] = row
}