distinct problem

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
grudz
Forum Commoner
Posts: 68
Joined: Thu Dec 04, 2003 12:52 pm

distinct problem

Post by grudz »

Hello,

i need to know not to show duplicates of a certain record within the column

example

question_title | question | reply (column names)

question1 | bla bla |
question1 | | reply

when i show my question, i dont want to see question1 twice

here is my code

Code: Select all

mysql_select_db($database_cnpa, $cnpa);
$query_forum = "SELECT DISTINCT * FROM forum ORDER BY `date` DESC";
$query_limit_forum = sprintf("%s LIMIT %d, %d", $query_forum, $startRow_forum, $maxRows_forum);
$forum = mysql_query($query_limit_forum, $cnpa) or die(mysql_error());
$row_forum = mysql_fetch_assoc($forum);
Chambrln
Forum Commoner
Posts: 43
Joined: Tue Dec 02, 2003 10:45 am
Location: Oregon

Post by Chambrln »

When you order the query order it by Question as well as date. Then just set a boolean variable to true once the question has been displayed. Check for the variable being set before you display the question and if it is then don't display it.

Code: Select all

<?php
mysql_select_db($database_cnpa, $cnpa); 
$query_forum = "SELECT DISTINCT * FROM forum ORDER BY `date` DESC, `question` ASC"; 
$query_limit_forum = sprintf("%s LIMIT %d, %d", $query_forum, $startRow_forum, $maxRows_forum); 
$forum = mysql_query($query_limit_forum, $cnpa) or die(mysql_error()); 

while ($row_forum = mysql_fetch_assoc($forum)) {
  if ((isset($prev_question) && $prev_question <> $row_forum['question']) || !isset($prev_question)) {
    echo $row_forum['question'];
    echo "<br>";
    echo $row_forum['answer'];
    $prev_question = $row_forum['question'];
  } else (isset($prev_question) && $prev_question == $row_forum['question']) {
    echo $row_forum['answer'];
  }
}

?>
Post Reply