baleeq123 Posted April 29, 2013 Share Posted April 29, 2013 (edited) let me tell you i am new to php and mysql i have mysql database like below UID Student name Date H 433100002 ABC 24-04-13 0.5 433100002 ABC 24-04-13 1 433100002 ABC 24-04-13 0 As you have seen UID,Student name,Date are repeating but the value is H is different , so i want to display the data in the form below UID Student Id Date Hour1 Hour2 Hour3 433100002 ABC 24-04-13 0.5 1 0 Anybody's help appreciated. Edited April 29, 2013 by baleeq123 Quote Link to comment Share on other sites More sharing options...
Barand Posted April 29, 2013 Share Posted April 29, 2013 (edited) SELECT uid, studentname, date, GROUP_CONCAT(hours) as hours FROM tablename GROUP BY uid, studentname, date which will give this for each student +-----------+-------------+----------+---------+ | uid | studentname | date | hours | +-----------+-------------+----------+---------+ | 433100002 | ABC | 24-04-13 | 0.5,1,0 | +-----------+-------------+----------+---------+ If you want the hours in separate table cells, explode the field on the commas Edited April 29, 2013 by Barand Quote Link to comment Share on other sites More sharing options...
baleeq123 Posted April 29, 2013 Author Share Posted April 29, 2013 SELECT uid, studentname, date, GROUP_CONCAT(hours) as hours FROM tablename GROUP BY uid, studentname, date which will give this for each student +-----------+-------------+----------+---------+ | uid | studentname | date | hours | +-----------+-------------+----------+---------+ | 433100002 | ABC | 24-04-13 | 0.5,1,0 | +-----------+-------------+----------+---------+ If you want the hours in separate table cells, explode the field on the commas can you describe how to explode on comma. thanks alot Quote Link to comment Share on other sites More sharing options...
Barand Posted April 29, 2013 Share Posted April 29, 2013 see manual - explode eg $hoursArray = explode(',', $row['hours']); foreach ($hoursArray as $hour) { echo "<td>$hour</td>"; } Quote Link to comment Share on other sites More sharing options...
baleeq123 Posted April 30, 2013 Author Share Posted April 30, 2013 If you want to show data in ASCENDING order You query should be like this: "SELECT * FROM `TABLE_NAME` ORDER BY `H`" ; If you want to show one row only then limit query to 1 "SELECT * FROM `TABLE_NAME` ORDER BY `H`" LIMIT 1; I want UID, Name, date should come one and H should contain all values like below UID Student Id Date H H H 433100002 ABC 24-04-13 0.5 1 0 Quote Link to comment Share on other sites More sharing options...
Barand Posted April 30, 2013 Share Posted April 30, 2013 I want UID, Name, date should come one and H should contain all values like below UID Student Id Date H H H 433100002 ABC 24-04-13 0.5 1 0 I've just shown you how - what are you stuck on now? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.