Jakob Termansen
Jakob Termansen

Reputation: 55

Getting two or more images ids from the same column

I got a MySQL table with one or more comma separated numbers in it. Those numbers refer to ID's in another MySQL table, witch also includes the name of a image.

I want to select a row where the field "images" contains "1,43" and display the two images with ID 1 and 43 from the 2nd table.

Do I have to have the two numbers in differents fields? I'd prefer keep them in the same one.

Upvotes: 4

Views: 127

Answers (1)

John Woo
John Woo

Reputation: 263693

use FIND_IN_SET. Assuming you have the table like this below,

Table1

+++++++++++
Images
+++++++++++
1,43

Table2

+++++++++++++++++
ImageID  Image
+++++++++++++++++
1        ....
43       ....

Sample Query:

SELECT  b.*
FROM    Table1 a
        INNER JOIN Table2 b
            ON FIND_IN_SET(b.ImageID, a.Images) > 0

If you have time to modify the structure of the table. Please do. The query above will perform slow if you are doing it on large databases.

Upvotes: 5

Related Questions