PDA

View Full Version : error with supposedly simple query.[SOLVED]


bazz
02-12-2008, 09:25 PM
The query below brings in a record for one row of the tbl_businessDetails but is repeated once, for every row in tbl_contact.
So I get the first three values correctly for the business but, they are repeated alongside the values for each individual record in tbl_contact.



my $sth = $dbh->prepare ("SELECT BD.contact_id, BD.group_name, BD.premises_name, CNT.Contact_ID, CNT.Phone_Number, CNT.Fax_Number, CNT.Web_URL, CNT.email_Address
FROM tbl_businessDetails BD, tbl_contact CNT
WHERE BD.group_name = '$selected_group_name'
AND BD.premises_name = '$selected_premises_name'

") or die "prepare statement failed: $DBO::errstr\n";


The WHERE conditions are met correctly, and they are for the single business entry in tbl_businessDetails.

Please tell me what I am not understanding.


Yup, it's me who is simple !! lol. I added a new AND conditioanl, to force BD.contact_id = CNT.Contact_ID It works now.


bazz

shyam
02-13-2008, 08:42 PM
Yup, it's me who is simple !! lol. I added a new AND conditioanl, to force BD.contact_id = CNT.Contact_ID It works now.


which is why its a good practise to use the join format to the select which encourages you to give the join condition immediately after the join

SELECT BD.contact_id, BD.group_name, BD.premises_name, CNT.Contact_ID, CNT.Phone_Number, CNT.Fax_Number, CNT.Web_URL, CNT.email_Address
FROM tbl_businessDetails BD inner join tbl_contact CNT on BD.contact_id = CNT.Contact_ID
WHERE BD.group_name = '$selected_group_name'
AND BD.premises_name = '$selected_premises_name'