Reputation: 3
If I have a database with a field called items
with '1,2,3,7,15,25,64,346'
in it.
Is there a function to use the commas as a separator and take each number and start a new query and get info for that id? Like a foreach
but in sql?
I can see it so clear but cannot put it in words...
Upvotes: 0
Views: 64
Reputation: 3848
It seems you want data like this where 'source' is table having the column
SELECT target.* FROM target
LEFT JOIN source ON FIND_IN_SET(target.id, source.ids_row)
Upvotes: 0
Reputation: 392
You can use this query,
SELECT * FROM TABLE_NAME WHERE COLUMN_NAME in (1,2,3,7,15,25,64,346);
Upvotes: 1
Reputation: 5090
There is so way to do a "SQL foreach". Do it using PhP
Also, having such fields means your database is not normalized. You should give a look at relational database normalization, otherwise the bigger your database will be, the bigger your problems will be.
Upvotes: 0
Reputation: 2437
You can use the query SELECT * FROM table_name WHERE id IN ( 1,2,3,4,5 )
to find data having id's.
No need to separate the values. This will give you the desired result.
Upvotes: 0
Reputation: 1258
Yes, there is. You can use command IN
mysql_query( 'SELECT * FROM `table` WHERE `id` IN ( 1,2,3,4,5 );' );
Upvotes: 1
Reputation: 13728
$row = '1,2,3,7,15,25,64,346'; // $row = $field['value']
$arr = explode(',',$row);
foreach($arr as $a) {
// write your query
}
Upvotes: 0