Saturday, 19 June 2010

sql alias and where clause syntax

after column alias, it can't use where clause directly
select it again


a wrong example:
select *, (YEAR(CURDATE())-YEAR(dob)) - (RIGHT(CURDATE(),5) As age from `main_tb` where age >= 21 and age <= 36;


the correct one (assign a new name to the new selected table by using table alias):
select * from
(select *, (YEAR(CURDATE())-YEAR(dob)) - (RIGHT(CURDATE(),5)
As age from `main_tb`)
as new_tb_name
where age >= 21 and age <= 36;

No comments:

Labels

Search This Blog