Hi all,
Thanks in advance for any help provided.
MySQL version: 4.0.20a-nt.
I have created normalised tables for individuals. I have created a seperate table to hold all work and home email addesses for individuals. There will also be cases where the individual will not have an email address or will have one or the other.
I am trying to compose a query that will return all data for individuals on a single row.
The following is the SQL statement I have created thus far: -
The results I am getting at the moment are :
What I am trying to achieve is: -
Is this possible?
If so what do I need to do to achieve the desired results? (I believe that my problem is in my join statements)
Thanks for any help,
Tom.
Create statements for the relevant tables: -
Thanks in advance for any help provided.
MySQL version: 4.0.20a-nt.
I have created normalised tables for individuals. I have created a seperate table to hold all work and home email addesses for individuals. There will also be cases where the individual will not have an email address or will have one or the other.
I am trying to compose a query that will return all data for individuals on a single row.
The following is the SQL statement I have created thus far: -
Code:
SELECT distinct
t.FirstName,
t.LastName,
--tbl.address_line_1,
--tbl.address_line_2,
--tbl.address_line_3,
--tbl.address_line_4,
--tbl.address_line_5,
emw.Email_Address as work_email,
emh.Email_Address as home_email
FROM
tbl_Members t
left join tbl_Members_has_tbl_Addresses tb
on (tb.tbl_Members_idtbl_Member=t.idtbl_Member )
left join tbl_Addresses tbl
on (tb.tbl_Addresses_idtbl_Addresses=tbl.idtbl_Addresses)
left join tbl_Members_has_tbl_Email_Address e
on (e.tbl_Members_idtbl_Member=t.idtbl_Member )
left outer join tbl_Email_Add emw
on (e.tbl_Email_Add_idtbl_Email_Add=emw.idtbl_Email_Add and
upper(emw.Home_Work)="W")
left outer join tbl_Email_Add emh
on (e.tbl_Email_Add_idtbl_Email_Add=emh.idtbl_Email_Add and
upper(emh.Home_Work)="H")
The results I am getting at the moment are :
Code:
Firstname Lastname Work_Email Home_Email
Tom McE Work_email@address null
Tom McE null Home_email@address
Code:
Firstname Lastname Work_Email Home_Email
Tom McE Work_email@address Home_email@address
If so what do I need to do to achieve the desired results? (I believe that my problem is in my join statements)
Thanks for any help,
Tom.
Create statements for the relevant tables: -
Code:
CREATE TABLE tbl_Members (
idtbl_Member INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
UserName VARCHAR(12) NULL,
Passwrd VARCHAR(12) NULL,
CFT_Number INTEGER UNSIGNED NOT NULL,
FirstName VARCHAR(20) NOT NULL,
LastName VARCHAR(20) NOT NULL,
Date_Joined DATE NULL,
PRIMARY KEY(idtbl_Member)
)
AUTO_INCREMENT = 1;
CREATE TABLE tbl_Members_has_tbl_Email_Address (
tbl_Members_idtbl_Member INTEGER UNSIGNED NOT NULL,
tbl_Email_Address_idtbl_Email_Address INTEGER UNSIGNED NOT NULL,
PRIMARY KEY(tbl_Members_idtbl_Member, tbl_Email_Address_idtbl_Email_Address),
INDEX tbl_Members_has_tbl_Email_Address_FKIndex1(tbl_Members_idtbl_Member),
INDEX tbl_Members_has_tbl_Email_Address_FKIndex2(tbl_Email_Address_idtbl_Email_Address)
);
CREATE TABLE tbl_Email_Address (
idtbl_Email_Address INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
Home_Work VARCHAR(1) NULL,
Email_Address VARCHAR(50) NULL,
PRIMARY KEY(idtbl_Email_Address)
);