views:

25

answers:

2

hey guys

i have two mysql tables which are linked together and i need to show my category list

table_stories

sid    ||   title   ||   time      ||  hometext  ||  associated
1      ||   test    ||  2010-07-19 ||  text----  ||  8-14-

table_topics

topicid     ||  topicname  ||  slug
1           ||  car        ||  car  
4           ||  wall       ||  wall  
8           ||  benz       ||  benz  
14          ||  ford       ||  ford  

now if i use this query to fetch story list :

 LEFT JOIN table_topics AS nto ON 
 (CONCAT(' ',COALESCE(ns.associated,'-'),'-') LIKE CONCAT('%',nto.topicid,'-%'))

then output would show stories with topic id : 8 , 4 , 14

you see that it counts topic id 4 as similar as 14

please concider that i cant change mysql table structure and i should find a work around for this problem

A: 

I'd do the following: Split associated on '-', check if the topic id is IN the resulting array.

Maxem
+2  A: 

Use a combination of the FIND_IN_SET and REPLACE functions:

LEFT JOIN table_topics AS nto ON FIND_IN_SET(nto.topicid, REPLACE(ns.associated, '-', ','))
OMG Ponies
perfect solution
Mac Taylor
... for weird database schema ;-)
zerkms
yeah , i agree :D
Mac Taylor