Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql函数输出作为where条件in子句的一部分_Mysql - Fatal编程技术网

Mysql函数输出作为where条件in子句的一部分

Mysql函数输出作为where条件in子句的一部分,mysql,Mysql,使用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_c

使用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
当我调用上述函数时,它返回逗号分隔的CustomerCodes,如下所示 ‘1’、‘2’、‘3’、‘4’

但是我需要使用outputGet_customerCodes函数在条件下生成where子句

测试用例:

期望值:

在执行上述查询时,mysql应该根据函数输出给我结果。实际查询如下所示

从my_表中选择*,其中客户代码为('1'、'2'、'3'、'4')

问题:

  • 是否可以使用mysql函数输出作为where的一部分 子句中的条件
  • 如果可能,请提供一个例子。如果不可能,给我一个替代方案 解决方案

    • 需要考虑的几点:

      • 结果被截断为组\u concat\u max\u len系统变量所给出的最大长度,
        ,请参阅
      • 避免将函数的参数命名为表的列,请参阅
      一个选项使用:


      请参阅。

      我遇到了一个类似的问题,并在
      查找集合中的的帮助下解决了它:

      从my_表中选择*在_集中查找_(客户_代码,获取_客户代码(1002))

      返回varchar(500)声明结果varchar(1000)注意,您的结果可能会被截断。你可以用一个。
      
       select * from my_table where customer_code IN (Get_customerCodes(CAST('1002' AS SIGNED)));
      
      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)