Home > Software engineering >  How to print the name of many ID query SQL
How to print the name of many ID query SQL

Time:02-12

I have 2 queries one is this:

select nameCode
from users
where id = 3

When this query is executed there are many nameCode like

9616,1234,2456,3678...etc

Now I have this other query:

select description
from rights
where nameCode = 1234

and when I execute it, it shows

1234 => "Rights to use car"

How can I make a query where it takes all the nameCode and shows me all the description?

Thank you in advance.

CodePudding user response:

I think you can try to use JOIN to make it, JOIN with users and rights by nameCode

SELECT description
FROM users u
INNER JOIN rights r
ON u.nameCode = r.nameCode
WHERE u.id = 3 

if you need to select more columns you can add columns that you expect to get.

another way you can try to use EXISTS

SELECT description
FROM rights u
WHERE EXISTS (
    SELECT 1
    FROM users u 
    WHERE u.nameCode = r.nameCode AND u.id = 3 
)
  • Related