mySQL query query

Lord-Nikon

Expert Member
Joined
Jul 22, 2008
Messages
2,511
Reaction score
0
Code:
SELECT UNIX_TIMESTAMP(NOW()) AS UNIX_now, 
UNIX_TIMESTAMP(CONCAT(opens_date,' ',opens_time)) as UNIX_opens, 
UNIX_TIMESTAMP(CONCAT(closes_date,' ',closes_time)) as UNIX_closes
FROM random_table
WHERE UNIX_now <= UNIX_closes AND UNIX_now >= UNIX_opens

Problem : UNIX_now doesn't exist as a column.

Tried :
1. WHERE `UNIX_now` ....
2. WHERE tablename.UNIX_now
3. WHERE [UNIX_now]

Can someone please be so kind as to explain to me why I can't do this and possibly provide a solution.

I ended up doing :

Code:
SELECT UNIX_TIMESTAMP(NOW()) AS UNIX_now, 
UNIX_TIMESTAMP(CONCAT(opens_date,' ',opens_time)) as UNIX_opens, 
UNIX_TIMESTAMP(CONCAT(closes_date,' ',closes_time)) as UNIX_closes 
FROM random_table
WHERE UNIX_TIMESTAMP(NOW()) <= UNIX_TIMESTAMP(CONCAT(closes_date,' ',closes_time)) 
AND UNIX_TIMESTAMP(NOW()) >= UNIX_TIMESTAMP(CONCAT(opens_date,' ',opens_time))
 
Last edited:
Maybe I'm missing something but there is no FROM clause in your query?
Code:
SELECT [...,...]
[b]FROM [][/b]
WHERE []
 
Last edited:
Maybe I'm missing something but there is no FROM clause in your query?
Code:
SELECT [...,...]
[b/]FROM [][/b]
WHERE []

Yes, I neglected to put it in because I don't feel like showing what I'm working on :D The problem at hand is not the FROM clause, it's the WHERE clause :D

EDIT: Ok, for arguments sake it's in now...
 
Not sure about mySQL (it's been a while) but in MS SQL you can't use "aliases" further down in your query, so you have to use whatever the original was. You can kinda get around it by using inner queries or temp tables though if you have some hectic calculations etc.
 
Not sure about mySQL (it's been a while) but in MS SQL you can't use "aliases" further down in your query, so you have to use whatever the original was. You can kinda get around it by using inner queries or temp tables though if you have some hectic calculations etc.

Crap, suspected something like that. I might just end up putting this into a STORED PROCEDURE for future reference. This query is going to get called a lot and will always stay the same. Results will vary, but query not. The rest (that I've left out of the query) kinda gets a bit hectic.
 
Crap, suspected something like that. I might just end up putting this into a STORED PROCEDURE for future reference. This query is going to get called a lot and will always stay the same. Results will vary, but query not. The rest (that I've left out of the query) kinda gets a bit hectic.

Yes, aliasing is normally okay in the select portion (MS SQL example):

Code:
select u.UserID,(select AnotherUserID from Users u2 where u2.AnotherUserID = u.UserID)
from Users u

What you might consider is using the "into" command to load the selected values into a temporary table, which would then create the column UNIX_now correctly so you could then use the where statement as required.

I would suggest however that you do most of your "where" stuff within the statement that will select into the temporary table otherwise you might end up with performance issues.
 
Code:
SELECT *
FROM (
	SELECT UNIX_TIMESTAMP(NOW()) AS UNIX_now, 
	UNIX_TIMESTAMP(CONCAT(opens_date,' ',opens_time)) as UNIX_opens, 
	UNIX_TIMESTAMP(CONCAT(closes_date,' ',closes_time)) as UNIX_closes
	FROM random_table
) a
WHERE UNIX_now <= UNIX_closes AND UNIX_now >= UNIX_opens
 
Or using my method :p

Copy/Paste FTW! :D

LOL! C/P is being used like crazy at the moment. Perhaps we could suggest this to the creators of MySQL? You never know, in the next release this might get supported :D
 
Solution:

SELECT * FROM (
SELECT UNIX_TIMESTAMP(NOW()) AS UNIX_now,
UNIX_TIMESTAMP(CONCAT(opens_date,' ',opens_time)) as UNIX_opens,
UNIX_TIMESTAMP(CONCAT(closes_date,' ',closes_time)) as UNIX_closes
FROM random_table ) foo
WHERE UNIX_now <= UNIX_closes AND UNIX_now >= UNIX_opens
 
Code:
SELECT *
FROM (
	SELECT UNIX_TIMESTAMP(NOW()) AS UNIX_now, 
	UNIX_TIMESTAMP(CONCAT(opens_date,' ',opens_time)) as UNIX_opens, 
	UNIX_TIMESTAMP(CONCAT(closes_date,' ',closes_time)) as UNIX_closes
	FROM random_table
) a
WHERE UNIX_now <= UNIX_closes AND UNIX_now >= UNIX_opens

Cr@p, you beat me too it :D
 
Code:
SELECT *
FROM (
	SELECT UNIX_TIMESTAMP(NOW()) AS UNIX_now, 
	UNIX_TIMESTAMP(CONCAT(opens_date,' ',opens_time)) as UNIX_opens, 
	UNIX_TIMESTAMP(CONCAT(closes_date,' ',closes_time)) as UNIX_closes
	FROM random_table
) a
WHERE UNIX_now <= UNIX_closes AND UNIX_now >= UNIX_opens

Excellent! Thank you!
 
Top
Sign up to the MyBroadband newsletter
X