我有一个包含整数和参数的表:
date timestamp
123 2013-07-22 16:33:17
123 2013-07-22 16:34:47
234 2013-07-20 16:33:15
332 2013-07-24 16:33:37
422 2013-07-21 10:13:11
422 2013-07-22 14:53:12
我运行以下查询以获取它所选择的不同数据的数量以及随机的一组不同数据:
SELECT COUNT(DISTINCT(data)) as count, (SELECT data FROM data_table WHERE
DATE(timestamp) >= '2013/07/22' AND DATE(timestamp) < '2013/07/23'
GROUP BY data ORDER BY rand() LIMIT 1) as data FROM data_table WHERE
DATE(timestamp) >= '2013/07/22' AND DATE(timestamp) < '2013/07/23'
我得到了:
count data
2 123
或
count data
2 422
现在我有另一个表,* text_table *,带有PRIMARY键数据。它看起来像这样:
data text
123 "hi"
234 "hey"
332 "bye"
422 "cya"
我需要查询来获取上一个查询的结果,再加上文本:
count data text
2 123 "hi"
或
count data text
2 422 "cya"
我试过了:
SELECT text_table.text AS text, COUNT(DISTINCT(data_table.data)) as count,
(SELECT data FROM data_table GROUP BY data ORDER BY rand() LIMIT 1) as data
FROM data_table LEFT OUTER JOIN text_table ON data_table.date = text_table.text
但无论数据是什么,它都会一直返回相同的文本。
答案 0 :(得分:1)
我是这样做的,以获得您需要的所有信息。
SELECT COUNT(dt.date) count, dt.date, tt.text FROM data_table dt
JOIN text_table tt ON tt.date = dt.date
GROUP BY dt.date, tt.text
现在您只需要对其进行过滤即可获得所需的数据。
像这样,SELECT COUNT(dt.date) count, dt.date, tt.text FROM data_table dt
JOIN text_table tt ON tt.date = dt.date
WHERE dt.timestamp > '2013/07/22' AND dt.timestamp < '2013/07/23'
GROUP BY dt.date, tt.text
你也可以获得前1名来获得第一行
谢谢, 奇科
答案 1 :(得分:0)
试试这个:
SELECT COUNT(DISTINCT(data_table.data)) as count,
(SELECT data FROM data_table GROUP BY data ORDER BY rand() LIMIT 1) as data
,text_table.text AS text
FROM data_table JOIN text_table ON data_table.date = text_table.text