php - 从php中的2个不同表获取信息时查询错误

标签 php postgresql

对于我的家庭作业,我必须从有关世界的信息数据库中编写查询。我在其中一个查询中遇到错误,我无法弄清楚原因。我遇到问题的查询的问题陈述是:

Find all official languages, the country for which it is spoken, and the percentage of speakers      
(percentage of speakers is calculated as percentage spoken times country population divided by 100). 
Order results by the total number of speakers with the most popular language first. (238 results)

当我尝试从网站运行查询时遇到的错误是:

Query failed: ERROR: missing FROM-clause entry for table "city" LINE 1: ...kers FROM lab2.country AS 
co JOIN lab2.country ON lab2.city.... ^

我为查询编写的代码是:

 case 11:
                    $q = "SELECT name, language, ((pecentage * population)/100) AS 
percentage_of_speakers FROM lab2.country AS co JOIN lab2.country ON lab2.city.country_code WHERE  
(is_official IS TRUE) ORDER BY percentage_of_speakers DESC";

                    $result = pg_query($q) or die ('Query failed: '. pg_last_error());
                    break;

我为这个查询获得的信息来自两个不同的表,而不是一个。我相信我必须使用 JOIN 语句才能从两个表中获取数据。这是正在使用的 2 个表。提前感谢您的帮助。

 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

                           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

最佳答案

我重新格式化了您的查询以便我可以阅读它,但我没有解决问题。以下是更好格式的外观:

SELECT 
  name, 
  language, 
  ((pecentage * population)/100) AS percentage_of_speakers 
FROM lab2.country AS co 
     JOIN lab2.country ON lab2.city.country_code
WHERE is_official
ORDER BY percentage_of_speakers DESC

查询中的问题是以下两个突出显示的部分:

FROM lab2.country AS co 
     JOIN lab2.country ON lab2.city.country_code
               ^^^^^^^    ^^^^^^^^^^^^^^^^^^^^^^

第一个问题是您试图将 lab2.country 加入到它自身 (lab2.country AS co JOIN lab2.country)。实际上,您可以在 SQL 中执行此操作,有时它确实很有用,但这不是您在这里需要的。您需要将 lab2.country 加入到 lab2.city 中。

更大的问题是连接中的 ON 表达式。你不能只是加入这样的值(value)。 连接谓词(即调用ON 关键字之后的部分)必须是一个表达式,其计算结果为 bool 值(即真或假)。它将两个表联系在一起。


要了解如何解决这个问题,我建议您研究PostgreSQL tutorial on joins .链接的教程使用 PostgreSQL 教程中的示例表提供了如何工作的示例(这些不是您问题中的表):

SELECT *
FROM weather INNER JOIN cities ON (weather.city = cities.name);

看看 (weather.city = cities.name) 如何提供一种“测试”表达式,可以对每个行组合运行以查看它们是否匹配?

希望这会解释联接的工作原理,以便您了解如何修复原始查询。

(顺便说一句,我强烈建议您习惯于在诸如 psql 或 PgAdmin-III 之类的工具中以交互方式测试您的代码。它们比一些随机的基于 Web 的查询工具要方便得多,并且通常会给出更好的错误信息。)

关于php - 从php中的2个不同表获取信息时查询错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25924304/

相关文章:

php - 在不影响搜索结果的情况下使 $_GET 搜索 URL(包含术语)SEO 安全?

sql - 如何在 PostgreSQL 中仅列出表的属性名称

sql - 两个日期之间的周数

ruby-on-rails - heroku db:push 错误未初始化常量 Sequel::Postgres::PGError

sql - 条件 SQL 计数

php - mysql查询计算整行中的空格或单词

php - Google Cloud Storage 上传错误 - strpos() : Empty needle

php - 我们可以在 Codeigniter 中连接多个数据库吗?

php - 如何在php中用_替换空格

sql - Postgres 重复键错误代码