Using the following tables as an example and the specified query as a basic query, I want to add a way to select only rows with maximum id! Without the need to fulfill the second request!
TABLE VEHICLES id vehicleName ----- -------- 1 cool car 2 cool car 3 cool bus 4 cool bus 5 cool bus 6 car 7 truck 8 motorcycle 9 scooter 10 scooter 11 bus TABLE VEHICLE NAMES nameId vehicleName ------ ------- 1 cool car 2 cool bus 3 car 4 truck 5 motorcycle 6 scooter 7 bus TABLE VEHICLE ATTRIBUTES nameId attribute ------ --------- 1 FAST 1 SMALL 1 SHINY 2 BIG 2 SLOW 3 EXPENSIVE 4 SHINY 5 FAST 5 SMALL 6 SHINY 6 SMALL 7 SMALL
And basic request:
select a.* from vehicle a join vehicle_names b using(vehicleName) join vehicle_attribs c using(nameId) where c.attribute in('SMALL', 'SHINY') and a.vehicleName like '%coo%' group by a.id having count(distinct c.attribute) = 2;
So, I want to select rows with specific attributes that match the name, but only one entry for each name that matches where the identifier is the highest!
So, the working solution in this example will return the following lines:
id vehicleName ----- -------- 2 cool car 10 scooter
if he used some kind of max on id
at the moment I get all the records for a cool car and scooter.
My database in the real world follows a similar structure and has 10 thousand records in it, so such a query, as above, can easily return 3000+ results. I limit the results to 100 lines to reduce lead time, as the results are used when searching on my site. The reason I have repetitions of “vehicles” with the same name but only with a different identifier is because new models are constantly being added, but I keep the older one for those who want to dig them out! But when searching by car name, I don’t want to return old cards only the newest ones that have the highest identifier!
The correct answer will be to adapt the above query, which I am currently using, and return to it only the lines in which the name matches, but has the highest identifier!
If this is not possible, suggestions on how I can achieve what I want without massively increasing the search execution time will be evaluated.