在php中编写查询多个表 [英] Writing query for multiple tables in php

查看:73
本文介绍了在php中编写查询多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为自己的作业写最后一个查询,但是现在我被卡住了.此查询要求我从2个表而不是1个表中获取信息.我对如何从两个表中获取此信息以及如何将它们放在一起感到困惑.这是我要编写的查询的描述.

I am writing my last query for my homework, but I am stuck on it right now. This query requires me to take information from 2 tables instead of 1. I am confused on how to get this information from both tables and how to put them together. Here is the description of the query that I am trying to write.

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)

这是我要用于此查询的表

Here are the tables that I am going to use for this query

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(内部)联接.所以你会得到类似的东西:

As mentionned in the above comment, you have to do an SQL (inner) join in this case. So you'll have something like:

SELECT name, indep_year, region 
FROM lab2.country 
JOIN lab2.country_language
ON lab2.country.country_code = lab2.country_language.country_code
WHERE …

这篇关于在php中编写查询多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆