mySQL query query

Lord-Nikon

Expert Member
Joined
Jul 22, 2008
Messages
2,511
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:

dequadin

Expert Member
Joined
May 9, 2008
Messages
1,434
Maybe I'm missing something but there is no FROM clause in your query?
Code:
SELECT [...,...]
[b]FROM [][/b]
WHERE []
 
Last edited:

Lord-Nikon

Expert Member
Joined
Jul 22, 2008
Messages
2,511
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...
 

rorz0r

Executive Member
Joined
Feb 10, 2006
Messages
7,968
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.
 

Lord-Nikon

Expert Member
Joined
Jul 22, 2008
Messages
2,511
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.
 

guest2013-1

guest
Joined
Aug 22, 2003
Messages
19,800
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.
 

dequadin

Expert Member
Joined
May 9, 2008
Messages
1,434

rorz0r

Executive Member
Joined
Feb 10, 2006
Messages
7,968
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
 

Other Pineapple Smurf

Honorary Master
Joined
Jun 21, 2008
Messages
14,593
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
 

Other Pineapple Smurf

Honorary Master
Joined
Jun 21, 2008
Messages
14,593
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
 

FarligOpptreden

Executive Member
Joined
Mar 5, 2007
Messages
5,396
If I were to do it in MSSQL, I would've put the timestamp data in a view... Simple, elegant and fast. :D
 

Lord-Nikon

Expert Member
Joined
Jul 22, 2008
Messages
2,511
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