Display data from two tables by grouping in PHP
I am new in php field. I have created 2 tables in Mysql 1.Group 2. Event. goup_id is primary key in group an开发者_开发问答d its foreign key in event.
I want to display events which is in specific group. Example : Group1 Event 1 Event 2
Group2
Event 3
Event 4 etc
In this way, first group title and then list of events in that group will come. I want to select data from 2 different table.
Please help!! Thanks
Something like this?:
$sql = "SELECT * FROM Event as e LEFT JOIN Group as g on g.group_id=e.group_id ORDER BY e.group_id";
$result = mysql_query($sql);
if($result){
$currGroup = -1;
while($row=mysql_fetch_array($result)){
if($row['groupName']!=$currGroup){
$currGroup = $row['groupName'];
echo $currGroup."\n";
}
echo $row['eventName']."\n";
}
}
精彩评论