Posts

Showing posts from August, 2021

MySQL: Updating all rows setting a field to 0, but setting one row's field to 1

 efficient way to update a selection of rows' field to 0, but set One of these rows to 1 based on an ID. UPDATE `table` SET `inuse` = (`id` = 23) WHERE <condition> Example: UPDATE `communication` SET `is_default` = (`id` = 1 and `empid` = 1) WHERE empid = 1 https://stackoverflow.com/questions/1013042/mysql-updating-all-rows-setting-a-field-to-0-but-setting-one-rows-field-to-1

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