php多个表查询的方法_在php中编写多个表的查询

我正在写我的作业的最后一个问题,但我现在仍然坚持下去.这个查询要求我从2个表而不是1个表中获取信息.我对如何从两个表中获取这些信息以及如何将它们放在一起感到困惑.以下是我要编写的查询的说明.

Find the name, independence year, and region of all countries where English is an official language.

Order results by region ascending and alphabetize the results within each region by country name.

(44 results)

以下是我将用于此查询的表

Table "lab2.country_language"

Column | Type | Modifiers

--------------+-----------------------+----------------------------------------

country_code | character(3) | not null default ''::bpchar

language | character varying(30) | not null default ''::character varying

is_official | boolean | not null default false

percentage | real | not null default 0::real

Indexes:

"country_language_pkey" PRIMARY KEY, btree (country_code, language)

Foreign-key constraints:

"country_language_country_code_fkey" FOREIGN KEY (country_code) REFERENCES country(country_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 |

解决方法:

如上面的注释中所提到的,在这种情况下,您必须执行SQL(内部)连接.所以你会有类似的东西:

SELECT name, indep_year, region

FROM lab2.country

JOIN lab2.country_language

ON lab2.country.country_code = lab2.country_language.country_code

WHERE …

标签:php,postgresql

来源: https://codeday.me/bug/20190928/1826225.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值