Forum Moderators: open

Message Too Old, No Replies

Mysql Query and Like statement

         

supermanjnk

4:36 pm on Jan 16, 2007 (gmt 0)

10+ Year Member



Here is my query
SELECT mod_article.*, community.name as cname, subcategories.name as cname
FROM mod_article, community, subcategories
WHERE
(mod_article.community=community.nId ¦¦ mod_article.community='')
AND mod_article.subcategory like ';110;'
AND mod_article.cId like '%;81;%'
AND mod_article.enabled ='1'
AND mod_article.expire >='2007-01-16'

Right now it grabs 350 results... all duplicates, There are three results that it should pull.
If i remove the subcategory information, it pulls three results like it should.

One small change I need to make is on
AND mod_article.subcategory like ';110;'
I would like mod_article.subcategory like ';subcategories.sId;' However this is taking subcategories.sId as a literal string and not trying to match.

ConfusedButCommitted

9:57 pm on Jan 17, 2007 (gmt 0)

10+ Year Member



does INSTR(str,substr) work?

[dev.mysql.com...]

ConfusedButCommitted

10:03 pm on Jan 17, 2007 (gmt 0)

10+ Year Member



or like ';$subcategories.sId;'

LifeinAsia

10:03 pm on Jan 17, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Where's your limiting JOIN between subcategories and the other tables? You're getting duplicates because you're pulling all your matches AND matching them against every row of subcategories.

You're also returning 2 columns with the same name ("cname")- bad design and may throw errors depending on your specific database.