Forum Moderators: coopster
I have a problem with a mysql query, I'll be glad if you can help
Have a table like this:
mysql >>>>> id name1 name2 value date
I want to list all rows by grouping "date" columns like this:
11.07.2007
name11 name12 5
name21 name22 3
name31 name32 4
12.07.2007
name41 name42 3
name51 name52 6
name61 name62 1
name71 name72 2
14.07.2007
.
.
.
I'm not an expert at sql queries, and also dont know if one must be an expert to handle this mysql query. I googled for it, browsed php.net, but couldnt find a solution.
I have one, however it's a little complicated,I can select unique dates and make them an array, then with queries in a for loop, can select the table with all dates (in array) seperately, but if there are 100 different dates, it makes 100 different mysql queries to list rows gruoping date columns.So I'm here to ask you another solution
Thanks
You can solve this problem using a combination of query and programming logic with PHP.
Rather than making MySQL do all the work, let PHP help out by doing the grouping:
<?php
$q = 'select * from table ...etc order by date';
$result = mysql_query($q);
$count = mysql_num_rows($result);
for($i = 0; $i < $count; $i++) {
$row = mysql_fetch_assoc($result);
$date = $row['date'];
if($i == 0 ¦¦ $prevDate!= $date) {
//start new group
}
//print values for that row
$prevDate = $date;
}
?>
Hope that helps
[edited by: darrenG at 5:57 pm (utc) on July 1, 2007]