我有一个名为“dealBusinessLocations”的字段(在表“dp_deals”中),其中包含逗号分隔格式的另一个表(dp_business_locations)的一些 id。
dealBusinessLocations
----------------------
0,20,21,22,23,24,25,26
我需要在查询的 in() 函数中使用这些值。
喜欢
select * from dp_deals as d left join dp_business_locations as b on(b.businessLocID IN (d.dealBusinessLocations) ;
正弦mysql不支持任何字符串爆炸函数,我创建了一个存储函数
delimiter //
DROP FUNCTION IF EXISTS BusinessList;
create function BusinessList(BusinessIds text) returns text deterministic
BEGIN
declare i int default 0;
declare TmpBid text;
declare result text default '';
set TmpBid = BusinessIds;
WHILE LENGTH(TmpBid) > 0 DO
SET i = LOCATE(',', TmpBid);
IF (i = 0)
THEN SET i = LENGTH(TmpBid) + 1;
END IF;
set result = CONCAT(result,CONCAT('\'',SUBSTRING(TmpBid, 1, i - 1),'\'\,'));
SET TmpBid = SUBSTRING(TmpBid, i + 1, LENGTH(TmpBid));
END WHILE;
IF(LENGTH(result) > 0)
THEN SET result = SUBSTRING(result,1,LENGTH(result)-1);
END IF;
return result;
END//
delimiter ;
该功能运行良好。
mysql> BusinessList( '21,22' )
BusinessList( '21,22' )
-----------------------
'21','22'
但是使用该函数的查询也不起作用。这是查询。
select * from dp_deals as d left join dp_business_locations as b on(b.businessLocID IN (BusinessList(d.dealBusinessLocations)));
我也尝试过对函数参数使用静态值,但没用
select * from dp_deals as d left join dp_business_locations as b on(b.businessLocID IN (BusinessList('21,22')));
使用从函数返回的值似乎存在一些问题。