bansal
bansal

Reputation: 109

Value from one table on basis of another table

I have two table as table1 and table2 given below:

enter image description here

enter image description here

I want to have the value of only those table_name from table1 which has there id in print_table column in table 2.

I have implemented the following query but it returns only one value:

SELECT * FROM print_tabel_permission_admin WHERE id IN (select print_table from secondary)

Upvotes: 0

Views: 34

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521674

Use FIND_IN_SET:

SELECT DISTINCT
    t1.table_name
FROM table1 t1
INNER JOIN table2 t2
    ON FIND_IN_SET(t1.id, t2.print_table) > 0;

enter image description here

Demo

You should probably move away from storing CSV data in your tables like this. Instead, break out the IDs in table2 onto separate rows. This will make your life easier.

Upvotes: 1

Related Questions