Reputation: 429
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 ?
Upvotes: 1
Views: 476
Reputation: 95
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:
...where Required_Items like '%Bat%'
and ...'%Water Bottle%'
...where Required_Items like '%Glove%'
and ...'%Water Bottle%'
(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:
__The_Table_______ __Thing_Items____ __Items_______________
Thing_ID Thing Thing_ID Item_ID Item_ID Item
-------- -------- -------- ------- ------- --------------
T1 Baseball T1 i1 i1 Ball
T2 Football T1 i2 i2 Bat
T3 Fishing T1 i5 i3 Shoulder Pads
T2 i1 i4 Worms
T2 i3 i5 Water Bottle
T2 i5
T3 i4
T3 i5
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
Upvotes: 2
Reputation: 2438
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.
Upvotes: 0
Reputation: 2390
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.
Upvotes: 0