So I am trying to return only the records that are set to default from a mapping table but it is setting all languages to default related to that item from that market. Bellow is the exact sql I am using.
SELECT `teams`.*,
`markets`.`title` AS `market`,
`markets`.`short_name`,
`market_countries`.*,
`countries`.`title` AS `country`,
`languages`.`title` AS `language`,
`languages`.`short_code`,
`status`.`title` AS `status`,
`team_types`.`title` AS `type`,
`market_languages`.*
FROM `teams`
JOIN `markets` ON teams.market_id = markets.id
LEFT JOIN `market_countries` ON markets.id = market_countries.market_id
LEFT JOIN `countries` ON countries.id = market_countries.cou开发者_如何学编程ntry_id
LEFT JOIN `languages` ON languages.id = teams.language_id
LEFT JOIN `status` ON teams.status_id = status.id
JOIN `team_types` ON team_types.id = teams.type_id
JOIN `market_languages` ON teams.market_id = market_languages.market_id
WHERE (market_languages.is_default = 1)
AND (teams.status_id = 3)
GROUP BY `teams`.`id`
ORDER BY `teams`.`order_id` ASC, `teams`.`status_id` ASC
now I am obtaining specific columns and joining on a market and on the language but there is another table that relates markets and languages and which is the default language. unfortunately the it is saying that both languages are default where in the db it is clearly just 1 language default for that market.
So after more tweeking I found my solution, initially I was doing the single join and hoping to get a good return on the language but but markets where wrong. So I did some searching on complex joins and I came to a good resource on on www.bryanwebconsulting.com/blog/ Outer Join on Multiple Fields.
So after reading that I modded the sql as follows:
SELECT `teams`.*,
`markets`.`title` AS `market`,
`markets`.`short_name`,
`market_countries`.*,
`countries`.`title` AS `country`,
`languages`.`title` AS `language`,
`languages`.`short_code`,
`status`.`title` AS `status`,
`team_types`.`title` AS `type`,
`market_languages`.*
FROM `teams`
INNER JOIN `markets` ON teams.market_id = markets.id
LEFT JOIN `market_countries` ON markets.id = market_countries.market_id
LEFT JOIN `countries` ON countries.id = market_countries.country_id
LEFT JOIN `languages` ON languages.id = teams.language_id
LEFT JOIN `status` ON teams.status_id = status.id
INNER JOIN `team_types` ON team_types.id = teams.type_id
INNER JOIN `market_languages` ON ( teams.language_id = market_languages.language_id
AND teams.market_id = market_languages.market_id)
WHERE (market_languages.is_default = 1)
AND (teams.status_id = 3)
GROUP BY `teams`.`id`
ORDER BY `teams`.`order_id` ASC, `teams`.`status_id` ASC
So yes you are able to do a more detailed join using the AND "blah blah ON (AND)".
Hope this helps others too and I think I could clean this up even more now.
精彩评论