0

我有一个带有整数和参数的表:

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

但是,无论数据是什么,它都会始终返回相同的文本

4

2 回答 2

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 名得到第一行

谢谢,奇科

于 2013-07-23T20:55:34.610 回答
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
于 2013-07-23T20:15:04.010 回答