We help IT Professionals succeed at work.

Requesting help with creating an SQL query to select similar records

76 Views
Last Modified: 2017-03-20
This is a little hard to explain but I will do my best.  I need to write/update the example query below  so that it will return the records requested, but also return records based on what was returned. In my example below, two records are returned with "carnumber 55". I want to show those 2 records, but also any other records with the same "carname". In my example below, 3 additional records are displayed because they have the same car name as those returned from the original query. Can someone assist?

Select id, carnumber, carname
from tblOne 
where carnumber = 55

What I want returned from the new query

id   |   carnumber   |   carname
1    |      55       |   bobs car
5    |      55       |   johns car
7    |      88       |   bobs car
8    |      77       |   bobs car
21   |      97       |   johns car

Open in new window

Comment
Watch Question

Muhammad BurhanManager I.T.
CERTIFIED EXPERT
Top Expert 2015

Commented:
try this
select * from table01
where carname = 'bobs car'
order by carnumber

Author

Commented:
@muhammad, I will not have the carname in my query, I will only have the carnumber, that's why I need to use the results from the initial query to get the carname.
Senior DBA
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
Thank you!