i have a query like this:
select name from tbl1 where id in ( select id from tbl2 where code in (12,13,14,15,16))
and then I get the names:
name1
name2
name3
etc...
How can I modify the query to get the codes used in the second select with the names, like this:
12 name1
13 name2
14 name3
15 name4
16 name5
2
Answers
Right now your query is selecting
name
from tbl1, so you just need to join those rows against tbl2. It looks like both tables have an id field, so that is what you can join on. You can then simplify your where because the codes are available after the joinAs you haven’t provided a db schema, I suggest
tbl1
andtbl2
are related viaid
field:id
column.See the demo.