用于获取多个不同值的MySQL查询
这是我的示例表,我需要获得每个日期的所有数据+所有不同的answer\u用户键用于获取多个不同值的MySQL查询,mysql,select,distinct,Mysql,Select,Distinct,这是我的示例表,我需要获得每个日期的所有数据+所有不同的answer\u用户键 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
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
以及正常关联数组中的所有单个值,就像您执行此操作时得到的一样
date DISTINCT(answer_userKey)
2012-07-30 3
2012-07-31 2
在这里尝试了一切:(我希望这是您的要求..这一个在oracle中有效
SELECT * FROM tbl_data
WHERE date BETWEEN '2012-07-29' AND '2012-08-01'
试试这个:
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')
如果您同时需要详细信息和聚合数据,则可能需要执行两次查询以获取这两组不同的数据,或者只需查询详细信息并在您使用的任何语言(可能在多维数组中)内构建聚合。例如,在伪代码中:
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;
解决它:)刚刚添加*,在日期之前,一切都像一个符咒!非常感谢。
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
}