Hi am using Mysql PHP combination i have 3 tables named
1. Student(nameid,name,percent)
2. Location(locid,area,state)
3. Refer(referid,locid,nameid) for MANY-TO-MANY Relationship which means
A Student have many Location records as well as A Location have Many students these relation is maintained by Refer table by specifying both table ID'S(STUDENT,LOCATION) and Records of both tables should not duplicates
MY TABLES
TABLE-STUDENT
NAMEID - NAME - PERCENT
N1 ---- nameA -------70
N2----- nameB -------63
N3----- nameC -------45
TABLE-LOCATION
LOCID --AREA-- STATE
L1-------areaA ----------P
L2------areaB --------- Q
L3-------areaC-------- R
TABLE-REFER
REFERID-- NAMEID-- LOCID
R1-------- N1------ L1
R2-------- N1------ L2
R3---------- N2------ L1
R4---------- N3------ L3
ACTUAL OUPUT
NAMEID-- NAME-- PERCENT --LOCID-- AREA-- STATE
N1----- nameA-----70------L1------areaA-- ---P
N1------nameA----70-------L2------areaB----- Q Duplicate STUDENT RECORD
N2------nameB----63-------L1-----areaA------ P
N3------nameC----45-------L3-----areaC------ R
Problem:
When i try to join three tables by sql join as follows
select * from student join refer on student.nameid=refer.nameid join location on location.locid=refer.locid
It display records which includes duplicate from student(if student records connected to more than one record from location) but i need only unique student record which have many location records like
NAMEID-- NAME-- PERCENT-- LOCID-- AREA-- STATE
N1----- nameA -----70------- L1-------- areaA---- P
--------------------------------L2-------- areaB---- Q NULLfor repeated records in Student
N2----- nameB--------63-------- L1-------- areaA----- P
N3------nameC--------45---------L3-------- areaC----- R
or
NAMEID-- NAME-- PERCENT-- LOCID-- AREA-- STATE---LOCID2-- AREA2-- STATE2
N1---------nameA--- -----70------ L1-------areaA----P------------L2----------areaB------Q Location should be combined with Student
N2-------- nameB----------63------ L1----- areaA------ P
N3-------nameC-----------45-------L3------ areaC---- R
Plz suggest me the way to achieve this by SQL statement or PHP code because i have to make a report from this Data like
REPORT:
NAME: nameA
PERCENT:70
AREA1:areaA
AREA2:areaB
STATE1:P
STATE2:Q
-----------------------------
NAME: nameA
PERCENT:63
AREA1:areaA
a
STATE1:P
THANKS IN ADVANCE
This seems to be a work for the application itself :
$results = array();
while ($data = $qry->fetch(PDO::FETCH_OBJ))
{
if (!isset($results[$data->NAME]))
$results[$data->NAME] = array('percent' => $data->PERCENT, 'areas' => array(), 'states' => array());
$results[$data->NAME]['areas'][] = $data->AREA;
$results[$data->NAME]['states'][] = $data->STATE;
}
// parse $results into your report...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With