Query Help

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
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Query Help

Post by psurrena »

Hello,

I'm having an odd problem. The query below is pulling from two tables. The table "category" has 11 records. When PHP displays the record in a loop, it will display "project_name" 11 times for each project. If I add another "category" record, it will display "project_name" 12 times. I only need / want each record to display once. Any help would be appreciated.

Code: Select all

SELECT project_id,project_name,category_full FROM project,category WHERE project_cat LIKE'%$cat%' ORDER BY project_name ASC
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Which ones are repeating?

Maybe you should try some specificity.

Code: Select all

SELECT project.project_id, project.project_name, category.category_full
I don't know your table structure though, so this may not be the solution.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Blind join will cause duplicates...
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Query Help

Post by califdon »

Code: Select all

SELECT project_id,project_name,category_full FROM project,category WHERE project_cat LIKE'%$cat%' ORDER BY project_name ASC
You are asking for a JOIN between two tables, project and category, without specifying what the conditions for the join are. Either use LEFT JOIN ... ON ... syntax or set the criteria in the WHERE clause.
salmanshafiq
Spammer :|
Posts: 2
Joined: Sat May 27, 2006 7:54 am
Contact:

Post by salmanshafiq »

Dear,

You have not use any of the joing between both table therefore it's showing that records Use below query

SELECT P.project_id,P.project_name,C.category_full FROM project P,category C WHERE P.project_cat LIKE'%$cat%' AND P.categoryID=C.categoryID ORDER BY project_name
ASC


C.CategoryID will be the CategoryID in Category Table and P.CategoryID will be the categoryID used in project table
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post by psurrena »

With a few changes it worked great. Thank you so much!
Post Reply