mysql 反向查詢 函式建立
阿新 • • 發佈:2019-02-14
某表中存的menuitem欄位值為"3,4",要求找出3和4對應的menuitem值:
SELECT name FROM tb_menuitem where CONCAT(',', '3,4', ',')like CONCAT('%,', CAST(id AS CHAR),',%') --id前後加個逗號反正4772的值用772就能查出來,作用同 find_in_set
在mysql workbench中搞了半天,建立函式的語法老是報錯,最後通關的一份記錄備忘,關鍵還是要正確使用DELIMITER
DELIMITER $$ DROP function IF EXISTS mealbooker.fGetMenuItemNameZh; $$ DELIMITER $$ CREATE FUNCTION mealbooker.fGetMenuItemNameZh( ItemIDs varchar(50)) RETURNS varchar(800) CHARSET gb2312 BEGIN DECLARE myres varchar(800); SET myres = ''; SELECT GROUP_CONCAT(name SEPARATOR ',') INTO myres FROM mealbooker.tb_menuitem where CONCAT(',', ItemIDs, ',')like CONCAT('%,', CAST(id AS CHAR),',%'); RETURN myres; END $$