0

SELECT COUNT(*) AS cnt FROM ( (SELECT 'used' FROM b_names_1) UNION ALL (SELECT 'used' FROM b_names_2)) AS t WHERE 'used' = 1

这个码字很好..

但我想按时间间隔对结果进行排序,所以当我试图在底部添加这一行时,它不起作用:

AND registration_date_time >= STR_TO_DATE( '2013-01-28 00:00:00', '%Y-%m-%d %H:%i:%s' ) AND registration_date_time < STR_TO_DATE( '2013-02-02 00:00:00', '%Y-%m-%d %H:%i:%s' )

这段代码看起来像这样:

SELECT COUNT(*) AS cnt FROM ( (SELECT 'used' FROM b_names_1) UNION ALL
(SELECT 'used' FROM b_names_2)) AS t
WHERE 'used' = 1 AND registration_date_time >= STR_TO_DATE( '2013-01-28 00:00:00', '%Y-%m-%d %H:%i:%s' ) AND registration_date_time < STR_TO_DATE( '2013-02-02 00:00:00', '%Y-%m-%d %H:%i:%s' )

它给了我以下错误:#1054 - 'where 子句'中的未知列'registration_date_time'

知道如何正确地将日期代码实现到 SQL 中吗?

4

1 回答 1

1

这些列必须来自某个地方,将它们添加到内部查询中:

SELECT COUNT(*) AS cnt 
FROM ( 
    (SELECT 'used', registration_date_time FROM b_names_1) 
    UNION ALL
    (SELECT 'used', registration_date_time FROM b_names_2)
) AS t
WHERE 'used' = 1 
AND registration_date_time >= STR_TO_DATE( '2013-01-28 00:00:00', '%Y-%m-%d %H:%i:%s' ) 
AND registration_date_time < STR_TO_DATE( '2013-02-02 00:00:00', '%Y-%m-%d %H:%i:%s' )
于 2013-02-02T16:06:58.887 回答