Why find_in_set works but IN clause

后端 未结 2 1315
长发绾君心
长发绾君心 2020-12-21 07:48

I have code like this.

DELIMITER $$
    CREATE PROCEDURE `sp_deleteOrderData`(orderid BIGINT(11))
    BEGIN
        DECLARE shipmentnumbers VARCHAR(1000);
           


        
相关标签:
2条回答
  • 2020-12-21 08:04

    FIND_IN_SET searches a string inside a set of string separated by a comma.

    • MySQL FIND_IN_SET

    But you don't need to use GROUP_CONCAT to concatenate the rows to be used in the IN clause, try this,

    SELECT  GROUP_CONCAT(a_cartonid) 
    FROM    t_carton
    WHERE   a_shipmentid IN
             (
                SELECT a_shipmentid
                FROM t_shipment 
                WHERE a_orderid = orderid
             )
    

    or use JOIN

    SELECT  GROUP_CONCAT(DISTINCT a.a_cartonid) 
    FROM    t_carton a
            INNER JOIN
            (
                SELECT a_shipmentid
                FROM t_shipment 
                WHERE a_orderid = orderid
            ) b ON a.a_shipmentid = b.a_shipmentid
    
    0 讨论(0)
  • 2020-12-21 08:15

    IN accepts a list or parameters to search, FIND_IN_SET accepts a string parameter containing a comma-separated list:

    SELECT  1 IN (1, 2, 3, 4)
    
    SELECT  FIND_IN_SET(1, '1,2,3,4')
    

    If you try to apply IN to a comma-separated string, it will treat it as a single parameter and will match it as a whole:

    SELECT  1 IN ('1,2,3,4')
    

    Of course, the string '1' is not equal to the string '1,2,3,4' so the query above returns false.

    0 讨论(0)
提交回复
热议问题