如何在 mysql 和 vb.net 中使用带有 IN 运算符的参数命令

How to use parameters command with IN operator in mysql and vb.net

我需要在 WHERE 子句中使用 IN 运算符,所以我用 SQL 语句尝试了下一个代码,但它只为我提供了第一个数字的记录:

MyVar_AccID= "110,112,113"

Dim MyVar_SqlStr_Main As String = "SELECT 
                                    FatoraID, 
                                    FatoraRef,
                                    FatoraCode, 
                                    FatoraDate, 
                                    (D.AccName) AS DName,
                                    FatoraQuan,
                                    CONCAT(CategoryName,' ',ProductName) AS ProdName,
                                    FatoraSalePrice, 
                                    (C.AccName) AS CusName,
                                    FatoraPurPrice,
                                    (R.AccName) AS ResoName,
                                    FatoraPalletQuan,
                                    FatoraPalletPrice,
                                    FatoraPurTotal, 
                                    FatoraCustomer, 
                                    FatoraDis,
                                    FatoraPlus,
                                    FatoraSaleTotal,
                                    FatoraDriver, 
                                    FatoraCarNo, 
                                    FatoraDriverCost, 
                                    FatoraDriverCostTotal1,
                                    FatoraDriverCostTotal2,
                                    FatoraDriverPrice,
                                    FatoraDriverPayStatus, 
                                    FatoraReso, 
                                    FatoraProduct,
                                    FatoraDetails1,
                                    FatoraDetails2,
                                    FatoraDetails3,


                                    (FatoraPalletQuan * FatoraPalletPrice) AS PalletTotalPrice 
                                    FROM tblfatora F 
                                    INNER JOIN tblproducts P ON
                                    P.ProductID = F.FatoraProduct
                                    INNER JOIN tblaccounts R ON
                                    R.AccID = F.FatoraReso
                                    INNER JOIN tblaccounts C ON
                                    C.AccID = F.FatoraCustomer
                                    LEFT JOIN tblaccounts D ON
                                    D.AccID = F.FatoraDriver
                                    INNER JOIN tblcategories CT ON
                                    CT.CategoryID = P.ProductCategory
                                    Where 
                                    (FatoraReso IN (@FatoraReso))
                                    
                                    ORDER BY FatoraDate DESC"


        xCmd = New MySqlCommand(MyVar_SqlStr_Main, Conn)

        xCmd.Parameters.Add("@FatoraReso", MySqlDbType.VarChar).Value = MyVar_AccID

我也试过:

Where 
                                (FatoraReso IN ("@FatoraReso"))

但没有给我结果,我该如何解决这个问题,请注意,我不知道代码的数量,所以可能是 (1,2,3,4,5) 或更少或更多。

您似乎想检查某个值是否属于以逗号分隔的列表。您不能使用 IN 执行此操作,它需要一个值列表,而不是单个值字符串。

通用解决方案使用字符串函数:

where concat(',', @fatorareso, ',') like concat('%,', fatorareso, ',%')

在MySQL中,您可以使用handy string function find_in_set():

where find_in_set(fatorareso, @fatorareso)