gpt4 book ai didi

mysql - 在 mySQL 和 VB.Net 中构造 Like 查询

转载 作者:行者123 更新时间:2023-11-28 23:28:49 25 4
gpt4 key购买 nike

我有 ListView 和多个过滤器作为复选框列表。一切正常,但 Like 子句不正常。如下图

select * from products where  category Like '% Apparels, Bluetooth%'

但应该是这样的

select * from products where  category Like '% Apparels%' or category Like '%Bluetooth%'

如何更改我的代码以正确构建我的查询?

Public Function buildWhereClause() As String
Dim constr As String = ConfigurationManager.ConnectionStrings("conio").ConnectionString
Dim query As String = "select * from products"
Dim joiner As String = " "
Dim condition As String = String.Empty
Dim priceCondition As String = String.Empty

For i = 0 To priceFilter.Items.Count - 1
If priceFilter.Items(i).Selected Then
Dim price As String = priceFilter.Items(i).ToString
priceCondition = String.Concat(priceCondition, joiner, String.Format("'{0}'", price))
If joiner = " " Then joiner = ", "
End If
Next

Dim categoryCondition As String = String.Empty
joiner = " "

For i = 0 To categoryFilter.Items.Count - 1
If categoryFilter.Items(i).Selected Then
Dim category As String = categoryFilter.Items(i).ToString
categoryCondition = String.Concat(categoryCondition, joiner, String.Format("{0}", category))
If joiner = " " Then joiner = ", "
End If
Next

Dim whereClause As String = String.Empty
joiner = " where "
If Not String.IsNullOrEmpty(priceCondition) Then
whereClause = String.Concat(whereClause, joiner, String.Format(" price_range IN ({0})", priceCondition)) ' and sub_category IN ({0})", condition.Substring(0, condition.Length - 1))
joiner = " and "
End If

If Not String.IsNullOrEmpty(categoryCondition) Then
whereClause = String.Concat(whereClause, joiner, String.Format(" category Like '%{0}%'", categoryCondition))
joiner = " and "
End If

'If Not String.IsNullOrEmpty(brandCondition) Then
' whereClause = String.Concat(whereClause, joiner, String.Format(" sub_category in ({0})", categoryCondition))
' joiner = " and "
'End If

Dim masterClause As String = String.Empty
masterClause = (query & whereClause)
End Sub

最佳答案

因此,您通过遍历 categoryFilter.Items 获得 categoryCondition="Apparels, Bluetooth" 您在这里可以做的是替换 ,使用 %' 或 category Like '% 那么一切都会很酷。考虑以下几点:

If Not String.IsNullOrEmpty(categoryCondition) Then
whereClause = String.Concat(whereClause, joiner, String.Format("category Like '%{0}%'", categoryCondition.Replace(",","%' or category Like '%"))
End If

关于mysql - 在 mySQL 和 VB.Net 中构造 Like 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38136801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com