Your technology certification is waiting. Enroll in Cloud Class ®
$stmt = $dbh->prepare("SELECT * FROM table WHERE city = ?");
foreach ($dbh->query("SELECT DISTINCT city FROM table;") as $row):
printf("<h1>City: %s</h1>", $row->city);
$stmt->execute( array($row->city) );
while ($row = $stmt->fetch()):
printf("<tr><td>%s</td><td>%s</td></tr>", $row->column1, $row->column2);
Open in new window
MySQL extends the use of GROUP BY so that the select list can refer to nonaggregated columns not named in the GROUP BY clause. This means that the preceding query is legal in MySQL. You can use this feature to get better performance by avoiding unnecessary column sorting and grouping. However, this is useful primarily when all values in each nonaggregated column not named in the GROUP BY are the same for each group. The server is free to choose any value from each group, so unless they are the same, the values chosen are indeterminate.
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
Please enter a first name
Please enter a last name
Must be at least 4 characters long.
Join and Comment
Your question, your audience. Choose who sees your identity—and your question—with question security.