MySQL "X AS Column"
a2jfreak
Houston, TX Member
I don't have a column named `curage` or `thenage` . . . I find them from the DOB and the missingDate. MySQL gripes and says I have no column named "curage."
[php]
SELECT `ID`,COUNT(*) AS `count`,
((YEAR(CURDATE()) - YEAR(dob)) - (RIGHT(CURDATE(),5) < RIGHT(dob,5))) AS `curage`
FROM `missing`
WHERE `firstName` LIKE "%%"
HAVING `curage`=5
GROUP BY ID
[/php]
[php]
SELECT `ID`,COUNT(*) AS `count`,
((YEAR(CURDATE()) - YEAR(dob)) - (RIGHT(CURDATE(),5) < RIGHT(dob,5))) AS `curage`
FROM `missing`
WHERE `firstName` LIKE "%%"
HAVING `curage`=5
GROUP BY ID
[/php]
0
Comments
Changed query, but still doesn't work. This is annoying. Any ideas?
Lose all the backticks in your query for a start!
[php]
SELECT `ID`,COUNT(*) AS `count`,
((YEAR(CURDATE()) - YEAR(dob)) - (RIGHT(CURDATE(),5) < RIGHT(dob,5))) AS `curage`
FROM `missing`
WHERE `firstName` LIKE "%%"
HAVING `curage`=5
GROUP BY ID
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'GROUP BY ID' at line 1
[/php]
You can't use "count" as the column name for the count because it's a reserved word, so I changed it to Count_All.
Then do this:
CREATE VIEW `age_view`
AS
SELECT
*,
COUNT(*) AS total,
((YEAR(CURDATE()) - YEAR(dob)) - (RIGHT(CURDATE(),5) < RIGHT(dob,5))) AS curage
FROM `missing`
WHERE `firstName` LIKE "%%"
You have an error in your SQL syntax.
Check the manual that corresponds to your MySQL server version for the right
syntax to use near 'VIEW `age_view` AS SELECT *,COUNT(*) AS total, ((YEAR(CURDATE()
[/php]
Possibly my MySQL version (3.23.49) is too old. Are VIEWs only supported in v4?
[php]
CREATE VIEW view_age
AS
SELECT dob,firstName,lastName
FROM missing
WHERE firstName LIKE "%%"
You have an error in your SQL syntax.
Check the manual that corresponds to your MySQL server version for the right
syntax to use near 'VIEW view_age AS SELECT dob,firstName,lastName FROM missing WHE
[/php]
[php]
SELECT *,
DATE_FORMAT(dob, '%b %e, %Y') AS dob,
DATE_FORMAT(missingDate, '%b %e, %Y') AS missingDate,
CAST(((YEAR(CURDATE()) - YEAR(dob)) - (RIGHT(CURDATE(),5) < RIGHT(dob,5))) AS UNSIGNED) AS curage,
CAST(((YEAR(missingDate) - YEAR(dob)) - (RIGHT(missingDate,5) < RIGHT(dob,5))) AS UNSIGNED) AS thenage
FROM missing
WHERE firstName LIKE "%%"
HAVING curage=15
AND thenage=3
[/php]
Query 2: (Same as query 1, except the results are ORDERED by the lastname and only twenty records are sent back at the time)
[php]
SELECT *,
DATE_FORMAT(dob, '%b %e, %Y') AS dob,
DATE_FORMAT(missingDate, '%b %e, %Y') AS missingDate,
CAST(((YEAR(CURDATE()) - YEAR(dob)) - (RIGHT(CURDATE(),5) < RIGHT(dob,5))) AS UNSIGNED) AS curage,
CAST(((YEAR(missingDate) - YEAR(dob)) - (RIGHT(missingDate,5) < RIGHT(dob,5))) AS UNSIGNED) AS thenage
FROM missing
WHERE firstName LIKE "%%"
HAVING curage=15
AND thenage=3
ORDER BY lastName
LIMIT 0,20
[/php]
Is there any way I can have only *1* query rather than 2? Or should I not be concerned about that hit to the DB?
I use mysql_num_rows($result) in PHP to see how many rows Query 1 gives me.