我可以在mysql中的as
语句中使用BETWEEN
或类似的东西。
SELECT * FROM status
WHERE date BETWEEN CONVERT_TZ('2013-11-01 00:00:00','GMT','UTC')
AND CONVERT_TZ('2013-11-07 23:59:59','GMT','UTC') AS firstweek
AND date BETWEEN CONVERT_TZ('2013-11-08 00:00:00','GMT','UTC')
AND CONVERT_TZ('2013-11-15 23:59:59','GMT','UTC') AS secondweek
AND date BETWEEN CONVERT_TZ('2013-11-16 00:00:00','GMT','UTC')
AND CONVERT_TZ('2013-11-22 23:59:59','GMT','UTC') AS secondweek
ORDER BY date DESC
我收到语法错误。如何使用BETWEEN
使用别名?
是否有其他方式每周命名,这将作为我的标识符。
答案 0 :(得分:2)
您正在为表达式添加别名,而不是选定列 你应该这样做:
SELECT t.* FROM
(SELECT s.*, CONVERT(...) as field1, CONVERT(...) as field2 from status) t
...then do what you want with fieldsXX...
答案 1 :(得分:1)
你可以尝试
SELECT * FROM date JOIN
(SELECT CONVERT_TZ('2013-11-01 00:00:00','GMT','UTC') AS w1start,
CONVERT_TZ('2013-11-07 23:59:59','GMT','UTC') AS w1end,
CONVERT_TZ('2013-11-08 00:00:00','GMT','UTC') AS w2start,
CONVERT_TZ('2013-11-15 23:59:59','GMT','UTC') AS w2end,
CONVERT_TZ('2013-11-16 00:00:00','GMT','UTC') AS w3start,
CONVERT_TZ('2013-11-22 23:59:59','GMT','UTC') AS w3end) AS weeks
WHERE date BETWEEN w1start AND w1end
OR date BETWEEN w2start AND w2end
OR date BETWEEN w3start AND w3end
ORDER BY date DESC
这至少是一个开始。
但是,如果您想告诉应用程序满足3个条件中的哪个条件,那么yiu可能会想要
SELECT date BETWEEN w1start AND w1end AS firstweek,
date BETWEEN w2start AND w2end AS secondweek,
date BETWEEN w3start AND w3end AS thirdweek,
whatevercolumsyouneed
FROM date JOIN
(SELECT CONVERT_TZ('2013-11-01 00:00:00','GMT','UTC') AS w1start,
CONVERT_TZ('2013-11-07 23:59:59','GMT','UTC') AS w1end,
CONVERT_TZ('2013-11-08 00:00:00','GMT','UTC') AS w2start,
CONVERT_TZ('2013-11-15 23:59:59','GMT','UTC') AS w2end,
CONVERT_TZ('2013-11-16 00:00:00','GMT','UTC') AS w3start,
CONVERT_TZ('2013-11-22 23:59:59','GMT','UTC') AS w3end) AS weeks
HAVING firstweek OR secondweek OR thirdweek
ORDER BY date DESC
(未经测试;我不确定HAVING
是否放置正确。)
答案 2 :(得分:0)
您在AS
子句中使用WHERE
。它只能在列列表和FROM
子句中使用。
SELECT a.somefield AS s, someexpr AS e
FROM table1 AS a, ...
WHERE ...