使用“LIKE”返回表行时遇到问题操作员
我对数据库编程很陌生,并且在数据库中进行搜索时遇到困难。我有一个表,其中有一列名为“Required_Items”,它只是一个用“;”分隔的必需项目的列表。我无法让服务器在查询时返回行:
'SELECT * FROM The_Table WHERE required_Items LIKE '%item1%' '
数据库似乎无法在列中找到该项目。问题是我希望能够返回包含所有项目的行。我会尝试这样的事情:
'SELECT *
FROM The_Table
WHERE Requiered_Items LIKE '%item1%' AND
Requiered_Items LIKE '%item2%' AND
Requiered_Items LIKE '%item3%' AND//etc...
知道要测试的这些“项目”的数量是可变的,我该如何做到这一点?
I am quite new at database programming and I am having trouble doing searches in my database. I have a table with a column named Required_Items, it is just a list of required items separated by ';'. I can't get the server to return the rows when querying :
'SELECT * FROM The_Table WHERE Required_Items LIKE '%item1%' '
It seems that the database can't find that item in the column. The problem is that I want to be able to return rows that contain ALL the items. I would try something like:
'SELECT *
FROM The_Table
WHERE Requiered_Items LIKE '%item1%' AND
Requiered_Items LIKE '%item2%' AND
Requiered_Items LIKE '%item3%' AND//etc...
How can I do that knowing that there will be a variable number of these "items" to test ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当我遇到这样的问题时,结果总是我的比较字符串根本不匹配。 (这并不是说它们看起来不匹配。)常见原因包括拼写错误、大小写问题、字符(尤其是空格)与看上去不符。您的“喜欢”比较中是否有空格?
你是如何将数据存入数据库的?您是否从 Word 或 Excel 复制它并将其粘贴到 SQL 查询生成器或类似的东西中?如果您不小心,这可能会导致问题。
当然,您知道所有“喜欢”的比较都必须匹配才能获取数据......?
以下是可能发生的情况的示例:
如果“必需物品”字段 =“蝙蝠、球、手套、帽子、头盔、水战”
那么这些都会失败:(
因为“Water Bottle”在数据库中拼写错误)
您可以通过在 where 子句中一次包含一项来解决此类问题,直到找到失败的一项。
对于可变数量的项目,按照您设置的方式使用数据(一个 csv 字段中的所有项目),如果您使用动态 sql,您的代码可能是最干净的。您可以在其中在可用字符串中构建查询并执行变量。搜索“动态 SQL”。
尽管如此,在关系数据库中存储此类数据的首选方法是在实体之间创建可维护的关系。如果您将项目分解为如下结构,您的数据将会更加友好:
这种结构将使处理未知数量的项目变得非常容易。
如果您无法解决此问题,请发布实际代码和实际数据(如果可以)。
斯科特
When I have problems like this it invariably turns out to be that my comparison strings simply don't match. (That's not to say they don't look like they match.) Common reasons are spelling mistakes, upper vs lower case issues, characters (particularly spaces) aren't what they appear to be. Do you have spaces in any of your 'like' comparisons?
How did you get the data into the database? Did you copy it from Word or Excel and paste it into the SQL query builder, or somethng of that nature? That can cause problems if you're not careful.
And of course you know that ALL of your 'like' comparisons must match in order to get data...?
Here's an example of what may be happening:
If the 'Required Items' field = 'Bat, Ball, Glove, Cap, Helmet, Water Battle'
then these will both fail:
(Because 'Water Bottle' is spelled incorrectly in the database)
You can troubleshoot for this kind of problem by having one item at a time in your where clause until you find the one that fails.
Regarding a variable number of items, using the data the way you have it set up (all items in one csv field) your code might be cleanest if you used dynamic sql. That's where you build a query in a string vaiable and execute the variable. Search for "Dynamic SQL".
All that said, the preferred method of storing this kind of data in a relational database is to create maintainable relationships between entities. Your data would be much friendlier if you broke the items out into a structure like this:
This structure would make handling unknown numbers of items very easy to deal with.
If you can't solve this, post actual code and actual data if you can.
Scott
您不能只是将“任意数量”的参数放入固定的 SQL 语句中,或者都是一个字符串,而您需要对整个字符串进行精确或部分匹配。如果您需要在每个查询中查询不同数量的项目,则需要一个软件中间层,它能够计算搜索词的数量并即时构建适当的 SQL 语句。
You can't just throw "any number" of arguments into a fixed SQL statement, or it's all one string and you require exact or partial match on the whole string. If you need to query for different numbers of items per query, you'll need a software middle layer, that is able to count number of search terms and construct the appropriate SQL statement on-the-fly.
这个查询看起来是正确的。
您也可以使用全文索引和全文查询来获取结果。
例如:
从表中选择 *,其中 contains(Columns_list,'item1')
考虑阅读 如何更快地获得结果。
This query looks right.
You can use Full text index and full text query's to get result too.
eg:
Select * from table where contains(Columns_list,'item1')
Consider reading how to get results faster.