DISTINCT not working right....

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

DISTINCT not working right....

Post by psychotomus »

I am only trying to get one of each article_id. It seems to be getting the whole database instead.
$sql = "



SELECT DISTINCT article_id, pqdb_articles.*, category_id, category_name, SUBSTRING_INDEX(article_text,' '," . CENTER_COLUMN_PREVIEW . ") AS text



FROM pqdb_articles, pqdb_categories


WHERE article_title LIKE '%$search%' OR article_text LIKE '%$search%'


AND category_id = article_cat ";
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: DISTINCT not working right....

Post by McInfo »

It will help greatly if you supply the schema and some sample data when asking database-related questions.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: DISTINCT not working right....

Post by Weirdan »

psychotomus wrote: DISTINCT not working right....
I am only trying to get one of each article_id. It seems to be getting the whole database instead.
It works exactly how it should: selecting unique rows. If you wanted distinct article_id's you shouldn't have put all other fields there.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: DISTINCT not working right....

Post by John Cartwright »

You probably want to remove the DISTINCT in favor of a GROUP BY article_id
foxmahesh
Forum Newbie
Posts: 5
Joined: Thu Mar 31, 2011 8:34 am

Re: DISTINCT not working right....

Post by foxmahesh »

you should use group by clause instead of DISTINCT
Post Reply