在postgresql中将三个表组合在一起

时间:2013-02-10 23:18:49

标签: database postgresql inner-join

问题是:"列出所有官方语言,其中包括该语言的发言人数(不是百分比)。对于表中的每条记录,还会显示国家/地区名称和资本。您的结果应首先按国家/地区名称(从A到Z)排序,然后按发言人数量(从最大到最小)排序。"

我拥有的表格是lab2.country,lab2.city和lab2.country_language。

$query5 = "SELECT c.name AS country, c.capital,l.language,c.population,l.percentage, ((c.population * (l.percentage * 0.01)) :: bigint) AS speakers

FROM lab2.country c INNER JOIN lab2.city ci ON (c.country_code = ci.country_code) INNER        JOIN lab2.country_language
ON (c.country_code = l.country_code)

^这就是我需要INNER JOIN lab2.country,lab2.city和lab2.country_language ^

WHERE l.is_official is true
ORDER BY c.name ASC, speakers DESC";

问题在于加入三个表格。第一个国家与城市,然后国家和country_language。主键是country_code。

1 个答案:

答案 0 :(得分:0)

"SELECT c.name AS country, ci.name AS capital, l.language,
((c.population * (l.percentage * 0.01)) :: bigint) AS speakers

FROM lab2.country c INNER JOIN lab2.country_language l     ON(c.country_code = l.country_code)     JOIN lab2.city ci ON(c.country_code = ci.country_code)

WHERE l.is_official is true AND (ci.id = c.capital)
ORDER BY c.name ASC, speakers DESC";

大胆的是我正在寻找的答案。这是country_language上的INNER JOINING国家,然后是已经INNER JOINED表的城市。谢谢!