PCHomepage
Programmer
I'm trying to come up with a query that will give me a range of years from a specific start to specific end, and then to auto-increment the list starting at another specific value. So far I've succeeded partially by getting the year values from an existing table but it does not have the full range needed so is there a way to do this?
I know I can do it in PHP but I really need an actual query to feed into an existing PHP function that expects a query. Here is what I have so far:
What I need as output is something like:
but what I am getting has the year limited to the existing values while the ID, due to the ORDER BY on Year, is not consecutive.
I know I can do it in PHP but I really need an actual query to feed into an existing PHP function that expects a query. Here is what I have so far:
SQL:
SELECT
(@cnt := @cnt + 1) AS ID, `Year`
FROM table_name AS t
CROSS JOIN (SELECT @cnt := '455') AS dummy
WHERE t.ID IS NOT NULL
GROUP BY t.Year
ORDER BY t.Year
What I need as output is something like:
Code:
ID Year
456 1899
457 1900
458 1901
459 1902
460 1903
and so on
but what I am getting has the year limited to the existing values while the ID, due to the ORDER BY on Year, is not consecutive.
Code:
ID Year
543 1937
700 1938
712 1939
726 1940
737 1941