Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 857547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:24:56+00:00 2026-05-15T08:24:56+00:00

Need your help guys in forming a query. Example. Company – Car Rental Table

  • 0

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?

  1. If Mercedes is available in Showroom, then fetch only that row. (i.e. row 1 in above example)

  2. 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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T08:24:57+00:00Added an answer on May 15, 2026 at 8:24 am

    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):

    SELECT x.id, x.name, x.status
      FROM (SELECT t.id,
                   t.name,
                   t.status,
                   CASE 
                     WHEN @car_name != t.name THEN @rownum := 1 -- reset on diff name
                     ELSE @rownum := @rownum + 1 
                   END AS rank,
                   @car_name := t.name -- necessary to set @car_name for the comparison
              FROM CARS t 
              JOIN (SELECT @rownum := NULL, @car_name := '') r
          ORDER BY t.name, t.status DESC) x  --ORDER BY is necessary for rank value
     WHERE x.rank = 1
    

    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:

    DROP TABLE IF EXISTS `example`.`car_status_type_code`;
    CREATE TABLE  `example`.`car_status_type_code` (
      `car_status_type_code_id` int(10) unsigned NOT NULL auto_increment,
      `description` varchar(45) NOT NULL default '',
      PRIMARY KEY  (`car_status_type_code_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need your help, For example I have a decimal type variable and I
hy guys, i really need your help. i've succesfully connected to ftp server via
I need your help to define a special case in XML schema: A sequence
I really need your help in my issue very quickly and it's too close
I've some questions .. and I really need your help. I have an application.
I need your expertise once again. I have a java class that searches a
.NET developers out there! Need your opinion here! I am now using Visual Assist
I need some resources that talk about how to design your software to be
I would like to know your experience when you need to take over somebody
What are your best usability testing tips? I need quick & cheap.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.