简体   繁体   中英

MySQL FIND_IN_SET OR IN only work for first integer?

This is not Duplicate question and I have read all other answer mention by my search title.

I have MySQL query that should fiend integer value from table field ' employee_id '.

My table: 在此处输入图片说明

My Query is:

SELECT `task_allotment`.`task_id`, `task_allotment`.`sub_proj`, `task_allotment`.`project_id`, `task_allotment`.`task_name`, `task_allotment`.`task_date`, `task_allotment`.`task_status`, `task_allotment`.`assign_by`, `project`.`provider_id` FROM `task_allotment` INNER JOIN `project` ON `task_allotment`.`project_id` = `project`.`project_id` WHERE FIND_IN_SET(9, `task_allotment`.`employee_id`) && `task_allotment`.`task_status` !="Complete" ORDER BY `task_allotment`.`task_id` DESC

It return empty result: 在此处输入图片说明

It work for first integer only but not working with any other integer. example:

if Query is:

SELECT `task_allotment`.`task_id`, `task_allotment`.`sub_proj`, `task_allotment`.`project_id`, `task_allotment`.`task_name`, `task_allotment`.`task_date`, `task_allotment`.`task_status`, `task_allotment`.`assign_by`, `project`.`provider_id` FROM `task_allotment` LEFT JOIN `project` ON `task_allotment`.`project_id` = `project`.`project_id` WHERE FIND_IN_SET(1, `task_allotment`.`employee_id`) && `task_allotment`.`task_status` !="Complete" ORDER BY `task_allotment`.`task_id` DESC

It return proper result: 在此处输入图片说明

You can see above if I query with integer that place beginning of " employee_id " using "FIND_IN_SET(13, task_allotment . employee_id )" it return proper result but if I query though any other integer return empty result. I am not sure if only work for first integer?

Please guide me how I get proper result using other integer.

It looks like the string stored in employee_id is not a proper comma separated value it contains spaces after each comma that is why it won't work see here .

SELECT FIND_IN_SET(5, '1, 2, 4, 5, 6'); // returns 0
SELECT FIND_IN_SET(CONCAT(' ',5), '1, 2, 4, 5, 6'); // returns index 4 but won't work for first value
FIND_IN_SET(1, REPLACE('1, 2, 4, 5, 6', ', ',',')) //  returns index 1, this will for all values

Its really a bad design to store relations in a single row , you should normalize your data and save each tuple (task_id, employee_id) in a single row in new table which is called as junction/pivot table, this way you won't have these issues

If you have spaces in integers like, I can see 1, 2, 4, 5, 7,.. , then it will not work. I had same kind of problem.

Solution: REPLACE function in mysql can help. Just replace all spaces in that particular field with blank string '' .

UPDATE `TABLENAME` SET `employee_id` = REPLACE(`employee_id`, ' ', '')

Note: Be careful next time while saving values with spaces.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM