Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中编写一个需要从数据库中的两个不同表获取信息的查询_Php_Postgresql - Fatal编程技术网

在php中编写一个需要从数据库中的两个不同表获取信息的查询

在php中编写一个需要从数据库中的两个不同表获取信息的查询,php,postgresql,Php,Postgresql,我正在为我的家庭作业写最后一个问题,但我现在被卡在上面了。此查询要求我从2个表而不是1个表中获取信息。我对如何从两个表中获取这些信息以及如何将它们放在一起感到困惑。下面是我试图编写的查询的描述 For each country display the capital city name and the percentage of the population that lives in the capital for each country. Sort the results from

我正在为我的家庭作业写最后一个问题,但我现在被卡在上面了。此查询要求我从2个表而不是1个表中获取信息。我对如何从两个表中获取这些信息以及如何将它们放在一起感到困惑。下面是我试图编写的查询的描述

For each country display the capital city name and the percentage of the population that lives in   
the capital for each country. Sort the results from largest percentage to smallest percentage.
对于这个问题,我相信我必须得到全国的人口,然后得到首都的人口,然后将他们分开,以得到居住在首都的人口的百分比。我只是不知道我将如何进行这个数学运算,尤其是当数据来自两个不同的表时。提前谢谢你的帮助。下面是我将用于此查询的表

Table "lab2.city"
Column    |         Type          |                     Modifiers                     
--------------+-----------------------+-----------------------------------------
 id           | integer               | not null default nextval('city_id_seq'::regclass)
 name         | character varying(35) | not null default ''::character varying
 country_code | character(3)          | not null default ''::bpchar
 district     | character varying(20) | not null default ''::character varying
 population   | integer               | not null default 0
Indexes:
"city_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
"city_country_code_fkey" FOREIGN KEY (country_code) REFERENCES country(counry_code) ON DELETE CASCADE


 => \d country
                           Table "lab2.country"
 Column      |         Type          |               Modifiers              

-----------------+-----------------------+--------------------------------------
country_code    | character(3)          | not null default ''::bpchar
name            | character varying(52) | not null default ''::character varying
continent       | continent             | not null
region          | character varying(26) | not null default ''::character varying
surface_area    | real                  | not null default 0::real
indep_year      | smallint              | 
population      | integer               | not null default 0
life_expectancy | real                  | 
gnp             | real                  | 
gnp_old         | real                  | 
local_name      | character varying(45) | not null default ''::character varying
government_form | character varying(45) | not null default ''::character varying
head_of_state   | character varying(60) | default NULL::character varying
capital         | integer               | 

为了从两个表中获取信息,您必须在一个公共因子上连接它们,在本例中为国家/地区代码。此外,您还可以像我在这里所做的那样,在查询中正确地执行百分比计算。最后,您可以在PostgrSQL中对别名列进行排序,方法是使用该列在查询中的顺序中的编号。这里是第二列,因此是2

SELECT `lab2`.`city`.`name`, ((`lab2`.`city`.`population`/`lab2`.`country`.`population`) * 100) AS `Population Percentage`
FROM `lab2`.`city`
JOIN `lab2`.`country`
ON `lab2`.`city`.`country_code` = `lab2`.`country`.`country_code`
WHERE `lab2`.`city`.`id` = `lab2`.`country`.`capital`
ORDER BY 2

注意,城市表中没有任何表示资本的内容,除非国家/地区列中的资本值应为城市id。我已经修改了查询以使用它,不知道它是否正确。

你在谷歌上搜索过加入SQL吗?@user3699735:你认为你很聪明,是吗?修改您的旧帖子,使其无法作为副本找到?这个问题似乎离题了,因为OP说的是给我一个codez。此外,OP此前也发布了这个问题。OP没有提供任何数据,也没有设置sqlfiddle。@user3699735:如果你没有表现出丝毫的学习动机,为什么你希望有人做你的家庭作业?你为什么要作弊?