Search This Blog

Thursday, August 12

MySQL in clause string to array

MySQL 5.5.32 Schema Setup:

CREATE TABLE LIST(`id` INT, `list` VARCHAR(5)); 

INSERT INTO LIST(`id`, `list`) VALUES(1, '1,2,3'),(2, '6,8');

CREATE TABLE users(`id` INT, `name` VARCHAR(4)); 

INSERT INTO users(`id`, `name`)VALUES(1, 'jack'),(2, 'john'),(3, 'jane'),(6, 'jim'),(8, 'jade');


Query 1:

SELECT (SELECT GROUP_CONCAT(user.name) FROM user WHERE instr(concat(' ,',list.list,','),concat(',',user.id,',')) > 0 )  FROM list


Results:

jack,john,jane  

jim,jade

No comments:

Post a Comment