Distinct columns

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
cgildenhuys
Forum Newbie
Posts: 8
Joined: Fri Feb 21, 2003 3:10 am

Distinct columns

Post by cgildenhuys »

I do a select query to find all info in a table, some entries might have the same info in a column. For instance seperate entries can have the same year. How can I display the result with only one year showing at the top and the consequent entries below and not every time the year with the entry, and so on?

Thanks
User avatar
AlphaWolf
Forum Newbie
Posts: 12
Joined: Sun Mar 23, 2003 8:49 pm

Post by AlphaWolf »

I hope I am not making this more complicated that it needs to be. It is hard to tell without looking at the structure of the db and the data. But I will give it a shot.

I think if you normalize your db once more it may make it easier. You mentioned the year sometimes is the same in the one table.

Make a new year table that simply has id & year for fields. This table would have no duplicates.

In your table that has the year in it now...change it to be a foreign key to the year table.

Then you can do a join statement to get the data.

Now for the fun part...

In psuedo code....

// connect to db, get data, etc.

while (query_result > 0) {
// set your variables = to the returned data here.
// ie: $year = $row["$year"];
etc etc

//set variable to the year field from your query.
var = $year; //you should only get 1 year per iteration of the while loop.
echo "$var"; //this is like the year heading for your page...

/*Here you are saying if the $year_id is the same as the year.id then print that record. Each time thru the loop the year changes thus you should get the different data for the different years. */
if ( $year_id == $var ){
echo "$field1, $field2, etc etc";
$var++;
}

}


This is not 100% obviously, but it may get you started in the right direction?

:?
Post Reply