Hi,
Just a check if I am doing these joins in the best way.
I am mostly curious about having to use the CROSS JOIN to make it work.
I have to join several tables together to get the complete data. The main issue I faced with this query is neigther the company or address tables have INNER, OUTER, or LEFT JOIN potential via the ‘ON’ clause.
SELECT
c.company_short_name as 'Company'
, CONCAT(
a.street_number
, ' '
, a.street_name
, ', '
, p.city_name
, ', '
, p.state_prov_abbr
, ', '
, ct.country_name
)
AS 'Address'
FROM
addresses as a
CROSS JOIN companies as c
INNER JOIN postal_codes p
ON p.postal_code = a.post_code
INNER JOIN countries as ct
ON ct.country_iso_code = p.country_iso_code
INNER JOIN companies2addresses as c2a
ON c2a.company_id = c.company_id
And c2a.address_id = a.address_id
it outputs something like:
[TABLE]
[TR]
[TD]Company[/TD]
[TD]Address[/TD]
[/TR]
[TR]
[TD]ABC Building[/TD]
[TD]11 Somewhere Rd., ACity, AProvince, ACountry[/TD]
[/TR]
[TR]
[TD]Bob’s Athletics[/TD]
[TD]12 NotHere Ct., ACity, AProvince, ACountry[/TD]
[/TR]
[/TABLE]
It produces the proper data but is there a better way to do this?
<!-- Select a specific compaies address -->
SELECT
c.company_short_name as 'Company'
, CONCAT(
a.street_number
, ' '
, a.street_name
, ', '
, p.city_name
, ', '
, p.state_prov_abbr
, ', '
, ct.country_name
)
AS 'Address'
FROM
addresses as a
INNER JOIN companies as c
ON c.company_short_name = 'ABC Building'
INNER JOIN postal_codes p
ON p.postal_code = a.post_code
INNER JOIN countries as ct
ON ct.country_iso_code = p.country_iso_code
INNER JOIN companies2addresses as c2a
ON c2a.company_id = c.company_id
And c2a.address_id = a.address_id
It ouputs somethin like:
[TABLE]
[TR]
[TD]Company[/TD]
[TD]Address[/TD]
[/TR]
[TR]
[TD]ABC Building[/TD]
[TD]11 Somewhere Rd., ACity, AProvince, ACountry[/TD]
[/TR]
[/TABLE]
I guess that I never expected to have use a CROSS JOIN?
Regards,
Steve