use PHP and MYsql. I have 2 tables like
-
table ‘tag’: has field ‘id’ and ‘tag’ (‘tag’ is varchar)
-
table ‘entry’: has fields ‘tags’ and other fields (‘tags’ is varchar that stores many words as tag, each word is separated by comma. Then I use function ‘explode’ to extract tags from string to store in array ‘$tags’. All tags from table ‘entry’ are stored in table ‘tag’.)
What I want is to select all ‘tag’s from table ‘tag’ which are tags in the same string of ‘tags’ of table ‘entry’ that has a given tag (variable $tag). But no idea, I just try to SELECT tag from table ‘entry’ which string in ‘tags’ contains a specified tag (variable $tag).My current code is
$count=0;
$sql="SELECT tags FROM entry WHERE tag LIKE '%$tag%'";
$result = mysql_query($sql);
while($data=mysql_fetch_array($result)){
if(mysql_num_rows($result)!=0){
$tags1=explode(',',$data['tags']);
for($loop=0; $loop<=count($tags1)-1; $loop++){
if(!in_array($tags1[$loop], $tags2)){
$count+=1;
$tags2[$count]=$tags1[$loop];
}
}
}
}
The difficulties you are having solving this problem all stem from the fact that you’re storing a list of tags in a single column.
You really need to add an intersection table, to contain the data for the many-to-many relationship between tags and entries. This is the proper way to design a relational database, and many SQL queries will become more efficient if you do that.
For example, to get all the tags from an entry that matches tag
$tag:I wrote a chapter to detail the practical problems caused by this design in my book, SQL Antipatterns Volume 1: Avoiding the Pitfalls of Database Programming.