Search code examples
mysqlwhere-in

How to use mysql WHERE IN for two separate tables


I have two tables

books_tbl:

blocks  side-bar   top-bar
 23,45   3,15      11,56

pages_tbl:

id title
1  ff
3
11
15

I want to select the rows from pages_tbl where pages id has included either blocks, side-bar or tob-bar columns in books_table.

How to process this?


Solution

  • It's usually not a good idea to store comma separated values in a single field. If you really cannot change your data structure, you could use a query like this:

    select p.id, p.title
    from
      pages_tbl p inner join books_tbl b
      on (
        find_in_set(p.id, b.blocks)
        or find_in_set(p.id, b.side-bar)
        or find_in_set(p.id, b.top-bar)
      )
    -- add where condition?
    group by p.id, p.title