0

我可以在 mysql 或类似的东西中使用asin语句吗?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?每周是否有其他命名方式,这将作为我的标识符。

4

3 回答 3

2

您正在为表达式添加别名,而不是选定的列
您应该像这样制作 ir:

SELECT t.* FROM  
(SELECT s.*, CONVERT(...) as field1, CONVERT(...) as field2 from status) t
...then do what you want with fieldsXX...  
于 2013-11-07T06:45:54.310 回答
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

这至少是一个开始。

但是,如果您想告诉应用程序满足三个条件中的哪一个,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放置正确。)

于 2013-11-07T06:56:14.637 回答
0

ASWHERE子句中使用。它只能在列列表和FROM子句中使用。

SELECT a.somefield AS s, someexpr AS e
  FROM table1 AS a, ...
  WHERE ...
于 2013-11-07T06:47:52.287 回答