简体   繁体   中英

How to use aliased column day in WHERE clause?

I have a question about this query:

  SELECT DAYOFYEAR(t.ts_start) AS 'day', YEAR(t.ts_start) AS year, t.o AS value FROM Cryptoforecast_ETH_BTC_1h AS t 

when I run it it shows the results. Now i want to filter for day adding a WHERE statement like this

SELECT DAYOFYEAR(t.ts_start) AS 'day', YEAR(t.ts_start) AS year, t.o AS value FROM Cryptoforecast_ETH_BTC_1h AS t WHERE 'day'=157

But it gies no result and a warning:

Truncated incorrect DOUBLE value: 'day'

What is the problem?

You can't use alias column in your WHERE clause. So, you aren't actually compare column instead string value with int.

WHERE 'day'=157

instead you could use

WHERE DAYOFYEAR(t.ts_start) = 157

Side note : You don't need to use single quote for your column alias.

Just repeat the call to DAYOFYEAR in the WHERE clause:

SELECT
    DAYOFYEAR(t.ts_start) AS day,
    YEAR(t.ts_start) AS year,
    o AS value
FROM Cryptoforecast_ETH_BTC_1h
WHERE DAYOFYEAR(t.ts_start) = 157;

Or, if you want to use the day alias, you may do so with an overloaded HAVING clause:

SELECT
    DAYOFYEAR(t.ts_start) AS day,
    YEAR(t.ts_start) AS year,
    o AS value
FROM Cryptoforecast_ETH_BTC_1h
HAVING day = 157;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM