Reputation: 85
select field_name,field_unit,field_range
from observation_template
where template_name='abc'
and field_name not in ('[wd, ef]')
I am having a list which i get from another sql query,eg: [wd,ef] ,i need to get the new list of data which is not in the list while i execute the sql query,list with sqare braces ,how can i eliminate braces and write query properly
Upvotes: 1
Views: 267
Reputation: 189
You can simply write a sub query in NOT IN clause, like :
select field_name,field_unit,field_range from observation_template where template_name='abc' and field_name NOT IN (sub query for the list)
Upvotes: 2
Reputation: 1179
Try stored procedure
DELIMITER $$
DROP PROCEDURE IF EXISTS sp_get_observation_template $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_get_observation_template`(
IN `p_field_name` VARCHAR(255)
)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT '查詢客服人員資訊'
BEGIN
SET @sql = CONCAT("
SELECT
`field_name`,
`field_unit`,
`field_range`
FROM observation_template
WHERE `template_name` = 'abc'
AND `field_name` NOT IN ('", p_field_name, "')");
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END $$
DELIMITER ;
How to call
CALL sp_get_observation_template("wd, ef");
Upvotes: 0
Reputation: 13026
You can use find_in_set()
with replace()
function available in mysql.
select field_name,field_unit,field_range from observation_template
where template_name='abc' and
find_in_set(field_name
, replace(replace(replace('[wd, ef]', '[', ''), ']', ''), ' ', '')
) = 0
try this dbfiddle.
Upvotes: 0
Reputation: 331
select field_name,field_unit,field_range from observation_template where template_name='abc' and field_name NOT IN("wd","ef");
Upvotes: 0