Need your help guys in forming a query.
Example.
Company – Car Rental
Table – Cars
ID NAME STATUS
1 Mercedes Showroom
2 Mercedes On-Road
Now, how do I select only one entry from this table which satisfies the below conditions?
-
If Mercedes is available in Showroom, then fetch only that row. (i.e. row 1 in above example)
-
But If none of the Mercedes are available in the showroom, then fetch any one of the rows. (i.e. row 1 or row 2) – (This is just to say that all the mercedes are on-road)
Using distinct ain’t helping here as the ID’s are also fetched in the select statement
Thanks!
MySQL doesn’t have ranking/analytic/windowing functions, but you can use a variable to simulate ROW_NUMBER functionality (when you see “–“, it’s a comment):
Ordering by status DESC means that “Showroom” will be at the top of the list, so it’ll be ranked as 1. If the car name doesn’t have a “Showroom” status, the row ranked as 1 will be whatever status comes after “Showroom”. The WHERE clause will only return the first row for each car in the table.
The status being a text based data type tells me your data is not normalized – I could add records with “Showroom”, “SHOWroom”, and “showROOM”. They’d be valid, but you’re looking at using functions like LOWER & UPPER when you are grouping things for counting, sum, etc. The use of functions would also render an index on the column useless… You’ll want to consider making a CAR_STATUS_TYPE_CODE table, and use a foreign key relationship to make sure bad data doesn’t get into your table: