Table 1 :
QUERY: Create table client (
applicationNo int primary key,
name varchar(20)
);
Insert statement: Insert into client values (1,'XYZ'),(1,'ABC'),(1,'DEF');
applicationNo | name 1 | XYZ 2 | ABC 3 | DEF
Table 2:
Query : Create table client (
applicationNo int,
phoneNo Bigint,
foreign key (applicationNo) references client (applicationNo),
primary key(applicationNO,phoneNo)
);
Insert into phoneNO values (1,999999),(1,888888),(2,777777),(3,666666),(3,555555);
applicationNo | phoneNo 1 | 999999 1 | 888888 2 | 777777 3 | 666666 3 | 555555
Can I retrieve the tuples by joining both the tables in such a way that get the following output, but using single query, also I'm using mysql 5.1
applicationNo | name | phoneNo1 | phoneNo2 1 | XYZ | 999999 | 88888 2 | ABC | 77777 | Null 3 | DEF | 66666 | 555555
Edited : extra information
I tried using this something called cross tab .But I'm not able to use the totalPhoneNo inside the case statement
SELECT applicationNo,count(phoneNo) as totalPhoneNo, SUM(CASE WHEN totalPhoneNo= 1 THEN phoneNO ELSE Null END) AS phoneNo1, SUM(CASE WHEN totalPhoneNO = 2 THEN phoneNo ELSE Null END) AS phoneNo2 FROM phoneNO GROUP BY applicationNo;