【问题标题】:How to use parameters command with IN operator in mysql and vb.net如何在 mysql 和 vb.net 中使用带 IN 运算符的参数命令
【发布时间】:2020-12-07 00:38:55
【问题描述】:

我需要在 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) 或更少或更多。

【问题讨论】:

标签: mysql sql string vb.net where-clause


【解决方案1】:

您似乎想检查一个值是否属于逗号分隔的列表。 IN 无法做到这一点,它需要一个值列表,而不是单个值字符串。

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

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

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

where find_in_set(fatorareso, @fatorareso)

【讨论】:

  • 请注意,这将使查询无法使用 fatorareso 上的索引
猜你喜欢
  • 2010-09-28
  • 1970-01-01
  • 2011-05-09
  • 2015-01-30
  • 2018-11-11
  • 1970-01-01
  • 2012-03-25
  • 1970-01-01
相关资源
最近更新 更多