I keep my customer phone and fax numbers in the same database table. Which isn’t abnormal.
Not all my customers have phone numbers, while others do not have fax numbers.
I need to view both phone and faxes, even if they’re empty.
In the tbl_customers table I have 2 specific columns dedicated to the record’s default phone and default fax. They simply store the record id from tbl_phonenumbers.
Here’s for phone:
SELECT c.customername, p.phonenumber
FROM `tbl_customers` c
LEFT JOIN `tbl_phonenumbers` p ON c.customerid = p.customerid
WHERE c.defaultphone = 22
Now I need to add the default faxnumber id, which follows the same idea as the default phone.
I’m not quite sure how to go about this. Of course, I could create an entirely new database table called ‘tbl_faxnumbers` and LEFT JOIN to that. But I’d rather keep one single table for phone and fax numbers.
Is this a possibility with the LEFT JOIN?
You can join the same table multiple times, as long as each join has a unique alias so the DB server can figure out WHICH of those extra versions of the table you’re talking about:
For your case, I’d add a “type” identifier field to the phone numbers table, so you can indicate which is a phone number and which is a fax number. Then you’d do your joins as: