我有一个带有整数和参数的表:
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 key data。它看起来像这样:
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
但是,无论数据是什么,它都会始终返回相同的文本。