Just asking out of general curiosity.
In an SQL/MySQL DB, how could a DB/table be structured ( and what would be the search string) so that a specific row( record) can be found using TAGs ( as in a blog)?
To clarify my question, here are some FAILED attempt at a solution:
STRUCTURE: the record has a column for TAGS, where a comma separated string of tags is stored.
SEARCH: SELECT * FROM theTable WHERE tags LIKE %some,string,of,tags%
syntax aside, this method fails simply because it would impossible to place the tagnames in the same order fro all records so as to match the %…% condition
I was thinking that another solution would have been SEARCH: SELECT * FROM theTable WHERE tags IN (‘some’,‘string’,‘of’,‘tags’)
but that fails because it would mean the tag could could only contain one tag name at a time, thus defeating the purpose of tags.
This might work, but I wondered if there was a more graceful solution
store the list of tags with a starting and trailing comma: ,this,is,a,list,of,tags,
SELECT * FROM theTable WHERE tags LIKE %,this% AND tags LIKE %,is,% AND tags LIKE %,list,% …etc…
it just seems that the sequence of LIKE %whatever% statement could get pretty convoluted
As always thanks to everyone, in advance, for sharing your wisdom
[quote=“dresden_phoenix, post:1, topic:200880, full:true”]I was thinking that another solution would have been SEARCH: SELECT * FROM theTable WHERE tags IN (‘some’,‘string’,‘of’,‘tags’)
but that fails because it would mean the tag could could only contain one tag name at a time, thus defeating the purpose of tags.[/quote]you were absolutely correct right up untill you got to the “defeating the purpose” part – that’s wrong
the “more graceful solution” as you put it, is to have a table where each entity gets as many rows as tags
for example –
foods
23 steamed broccoli
24 boiled carrots
25 ice cream
26 gala apples
27 cilantro
food_tags
23 vegetable
23 side dish
24 vegetable
24 side dish
25 dessert
26 fruit
26 dessert
26 snack
27 herb
r937 (and Dave, I assume you meant the same solution)
would that not be considered storage intensive… I mean the TAG table would be orders of magnitude bigger than the DATA table? Again just curious if that’s a normal ‘cost of operation’ , if you will.
In terms of rows, sure, but in terms of storage, not so much. The tags table would be carrying a handful of characters plus a numeric field for each tag. In your way, you’d have a varchar field (assuming you’re smart enough to use that) which would have a larger varchar field which would hold the length PLUS the commas to separate them, so the larger you go, the less the difference would be.
Like in Rudy’s case, gala apples has three rows with no more than 7 characters plus an integer in a row. In your example, you’d have one field which would have 20 characters in it (the seventeen combined characters for the tags, plus the commas to separate them). So the difference would be negligible in the long run.
duh… sorry brain f-rt. I realize now that there are 3 tables involved:
an article table a food_tag table with reference for each tag applied to each row( the first column in the example), and the tags table. I thought in the example that the tag table was were the articles would be stored…