使用GROUP_CONCAT在mysql中创建了一个新函数。 我的功能如下:
CREATE FUNCTION `Get_customerCodes`(customer_id int(11))
RETURNS varchar(500) CHARSET utf8
BEGIN
Declare Result VARCHAR(1000);
set Result = (select GROUP_CONCAT(concat('\'', customer_code,'\'')
SEPARATOR ',') from customers
where customer_id in (customer_id));
Return Result;
END
当我调用上面的函数时,它会返回这样的逗号分隔的客户代码 ' 1'&#39 2'&#39 3'' 4'
但是我需要在 IN 条件的where子句中使用输出 Get_customerCodes 函数。
测试用例:
select * from my_table where customer_code IN (Get_customerCodes(CAST('1002' AS SIGNED)));
期望:
在执行上述查询时,mysql应该根据函数输出给出结果。实际查询看起来像下面
从my_table中选择*,其中customer_code为IN(' 1',' 2',' 3',' 4') ;
问题:
答案 0 :(得分:0)
要考虑的几点:
The result is truncated to the maximum length that is given by the group_concat_max_len system variable,
,请参阅12.19.1 Aggregate (GROUP BY) Function Descriptions :: GROUP_CONCAT。使用13.5 Prepared SQL Statement Syntax的选项:
mysql> DROP TABLE IF EXISTS `my_table`, `customers`;
Query OK, 0 rows affected (0.00 sec)
mysql> DROP FUNCTION IF EXISTS `Get_customerCodes`;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `customers`(
-> `customer_id` INT NOT NULL,
-> `customer_code` VARCHAR(2),
-> PRIMARY KEY(`customer_id`, `customer_code`)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `my_table`(
-> `id` INT NOT NULL,
-> `customer_code` VARCHAR(2)
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE FUNCTION `Get_customerCodes`(`_customer_id` INT)
-> RETURNS VARCHAR(500) CHARSET utf8
-> RETURN (
-> SELECT GROUP_CONCAT(CONCAT('\'', `customer_code`, '\'')
-> SEPARATOR ',')
-> FROM `customers`
-> WHERE `customer_id` IN (`_customer_id`)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO `customers`
-> (`customer_id`, `customer_code`)
-> VALUES
-> (1002, 1), (1002, 2),
-> (1002, 3), (1002, 4);
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> INSERT INTO `my_table`
-> (`id`, `customer_code`)
-> VALUES
-> (1, 1), (2, 2),
-> (3, 3), (4, 4);
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> SET @`query` := CONCAT('
'> SELECT *
'> FROM `my_table`
'> WHERE `customer_code` IN (',
-> `Get_customerCodes`(CAST('1002' AS SIGNED)), ')');
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @`query` `to_execute`;
+----------------------------------------------------------------------+
| to_execute |
+----------------------------------------------------------------------+
|
SELECT *
FROM `my_table`
WHERE `customer_code` IN ('1','2','3','4') |
+----------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> PREPARE `stmt` FROM @`query`;
Query OK, 0 rows affected (0.00 sec)
Statement prepared
mysql> EXECUTE `stmt`;
+----+---------------+
| id | customer_code |
+----+---------------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
+----+---------------+
4 rows in set (0.00 sec)
mysql> DEALLOCATE PREPARE `stmt`;
Query OK, 0 rows affected (0.00 sec)
请参阅db-fiddle。
答案 1 :(得分:0)
我遇到了类似的问题,并在FIND_IN_SET
的帮助下得以解决:
SELECT * FROM my_table WHERE FIND_IN_SET(customer_code, Get_customerCodes(1002));