【问题标题】:Writing a query that requires getting information from 2 different tables in a database in php [duplicate]在php中编写一个需要从数据库中的2个不同表中获取信息的查询[重复]
【发布时间】:2014-09-19 04:21:11
【问题描述】:

我正在为我的作业写最后一个查询,但我现在被困在它上面。此查询要求我从 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.

对于这个查询,我相信我必须得到整个国家的人口,然后得到首都的人口,然后将它们除以得到居住在首都的人口百分比。我只是想不通我将如何执行这个数学运算,尤其是当数据来自 2 个不同的表时。我在这里先向您的帮助表示感谢。这是我将用于此查询的表

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               | 

【问题讨论】:

  • 你用谷歌搜索了JOIN IN SQL 吗?
  • @user3699735:你认为你很聪明,是吗?修改您的旧帖子,使其无法被发现为重复? stackoverflow.com/revisions/25916257/1
  • 这个问题似乎是题外话,因为 OP 说“给我 teh codez”。此外,OP 之前已经发布过这个问题。 OP 没有提供数据,也没有设置 sqlfiddle。
  • @user3699735:如果你连一点学习的动力都没有,为什么还要指望别人帮你做作业?你为什么要作弊?

标签: php postgresql


【解决方案1】:

为了从两个表中获取信息,您必须将它们连接到一个公共因子上,在本例中为 country_code。此外,您可以像我在这里所做的那样在查询中执行百分比计算。最后,您可以在 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。我已经修改了查询以使用它,不知道它是否正确。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    • 2014-06-10
    相关资源
    最近更新 更多