Skip to content
Advertisement

SQL query to get rows contains value of another query

I have 2 tables transaction and query. Transaction table have unique tid and the query table has tid_(subtransactionid).

I want to get the list of all subtransactions by using tid.

I tried this

select *
from queries, transactions
where queries.id like 'transactions.tid%' and transactions.uid = 'NfPgWM1igYh2y2hDKrLWLOxyI6u1'

Advertisement

Answer

You can join the tables and use the operator LIKE in the ON clause:

select q.* 
from queries q inner join transactions t
on q.tid like concat(t.tid, '%') 
where t.uid = 'NfPgWM1igYh2y2hDKrLWLOxyI6u1'

If your database does not support the function concat() then you can use either:

on q.tid like t.tid || '%'

or:

on q.tid like t.tid + '%' 
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement