I have attached my data and need result, I need to filter data from the array list. I have show "2" data result.
How can I get my output in mysql data?
I have attached my data and need result, I need to filter data from the array list. I have show "2" data result.
How can I get my output in mysql data?
May be this code helps you
Sample code : SELECT FIND_IN_SET('1', '1,2,3,4,5')
Your code may like : SELECT * from table WHERE FIND_IN_SET('2', array)>0
Sadly There is no native Array Datatype in mysql See this How to store arrays in MySQL?
The best bet you can do is to go for Postgres where it has a support for it
Or anyway, you can create a text datatype and save the array as json. As for the getting the data set. You need to do it manually on php. There's no way to do that in MYSQL alone
Or much better yet Normalize again your table into 3NF Separate the ARRY into separate table and bound it using the ID
You can use LIKE
to do it
SELECT t.* FROM yourtable t
WHERE t.attr LIKE CONCAT('2,%')
OR t.attr LIKE CONCAT('%,2')
OR t.attr LIKE CONCAT('%,2,%')
Test data
CREATE TABLE `system_users` (
`id` varchar(32) NOT NULL,
`name` varchar(32) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into `system_users`(`id`,`name`)
values
('1','1,5,3,12'),
('2','1,2,3'),
('3','1,12,13'),
('4','1,5,2,10,12'),
('5','2,3,4'),
('6','5,4,3,2');
Working query:
SELECT * FROM system_users u
WHERE u.name LIKE CONCAT('2,%')
OR u.name LIKE CONCAT('%,2')
OR u.name LIKE CONCAT('%,2,%');
Output result:
+----+-------------+
| id | name |
+----+-------------+
| 2 | 1,2,3 |
| 4 | 1,5,2,10,12 |
| 5 | 2,3,4 |
| 6 | 5,4,3,2 |
+----+-------------+
Also,as Praveen S mentioned,FIND_IN_SET() also works for you,it's more simple and elegant then LIKE