我的SQL并没有将我的列表限制为我想要的实例

时间:2010-02-01 03:51:43

标签: sql mysql

所以我试图仅从映射表中返回设置为默认值的记录,但它将所有语言设置为与该市场中该项目相关的默认值。 Bellow是我正在使用的确切sql。

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.country_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

现在我正在获取特定的专栏并加入市场和语言,但还有另一个与市场和语言相关的表格,这是默认语言。不幸的是,它说这两种语言都是默认的,在数据​​库中,它显然只是该市场的1种语言默认值。

1 个答案:

答案 0 :(得分:0)

所以经过更多的推文后我发现了我的解决方案,最初我是在进行单一加入,并希望获得良好的语言回报,但是市场错误。所以我做了一些关于复杂连接的搜索,我在www.bryanwebconsulting.com/blog/多个字段上的外连接上找到了一个很好的资源。

所以在阅读之后我按如下方式修改了sql:

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

所以是的,您可以使用AND“blah blah ON(AND)”进行更详细的连接。

希望这对其他人也有帮助,我想我现在可以更加清理它了。