简体   繁体   English

SQL语句计算字段

[英]SQL statement calculated field

This is my question, I need help with the last bullet point. 这是我的问题,我需要最后一点的帮助。 I can't figure out how to create a calculated field that calculates what the field name suggests. 我不知道如何创建一个计算字段来计算字段名称的含义。

From the City table select ID, name, Country and Population and from the Country table select Life Expectancy, Name, SurfaceArea, GNP. 从“城市”表中选择ID,名称,“国家”和“人口”,然后从“国家”表中选择“预期寿命”,“名称”,“ SurfaceArea”和“ GNP”。 Restrict your result set using the following criteria( all of the following criteria must be met for records to be returned): 使用以下条件限制您的结果集(要满足所有条件,必须满足以下所有条件才能返回记录):

  • Country SurfaceArea between 3,000,000 and 40,000,000 (using the 乡村面积介于3,000,000到40,000,000之间(使用
    between operator) 运营商之间)

  • The length of the City.District field is greater than 4 City.District字段的长度大于4

  • Create the calculated field'CityPopulationPercentageOfCountryPopulation' that calculates what the field name suggests. 创建计算出的字段“ CityPopulationPercentageOfCountryPopulation”,以计算字段名称建议的内容。

This is what I have so far. 到目前为止,这就是我所拥有的。 Thanks in advance. 提前致谢。

SELECT City.ID
     , City.name
     , City.Country
     , City.Population
     , Country.LifeExpectancy
     , Country.Name
     , Country.SurfaceArea
     , Country.GNP
     , ROUND(City.Population * 100.0 / Country.Population, 1) AS 'CityPopulationPercentageOfCountryPopulation'
  FROM City
     , Country
 WHERE Country.SurfaceArea BETWEEN 3000000 AND 40000000 
   AND LENGTH(City.District) > 4

Country table 国家表

City table 城市表

Never use commas in the FROM clause. 请勿FROM子句中使用逗号。 Always use proper, explicit, standard JOIN syntax. 始终使用正确,明确,标准的JOIN语法。 Your query should look like this: 您的查询应如下所示:

 SELECT . . .
 FROM City JOIN
      Country
      ON City.CountryCode = Country.Code  -- or whatever columns are used for the `JOIN`
 WHERE Country.SurfaceArea BETWEEN 3000000 AND 40000000 AND
     LENGTH(City.District) > 4

Seems like a semi-cross join here. 似乎在这里是半交叉联接。 That is, every row returned from City is matched with every row returned from Country . 也就是说,从City返回的每一行都与从Country返回的每一行都匹配。

I recommend ditching the old school comma syntax for the join operation, and use the JOIN keyword instead. 我建议放弃用于JOIN操作的旧式逗号语法,而改用JOIN关键字。 If we're intending a semi-Cartesian product, I'd recommend including the CROSS keyword to alert future readers that the omission of any join predicates (conditions to match on) in the ON clause is intentional, and not an oversight. 如果我们打算使用半笛卡尔乘积,则建议添加CROSS关键字,以提醒以后的读者,在ON子句中省略任何连接谓词(匹配条件)是有意的,而不是疏忽大意。

Also, the LENGTH function returns number of bytes; 同样, LENGTH函数返回字节数; use CHAR_LENGTH to return a count of characters. 使用CHAR_LENGTH返回字符数。


I suspect we want to "match" each City to a particular Country. 我怀疑我们想将每个城市“匹配”到特定国家。 Maybe the City table contains a Country_ID column foreign key referencing ID column in the Country table. 也许City表在Country表中包含一个Country_ID列外键引用ID列。 Or maybe the Country column is the foreign key. 或者“ Country列是外键。 (That's just a guess; there's no conclusive indication this is the case in the question.) (这只是一个猜测;没有确凿的迹象表明问题就是这种情况。)

SELECT ci.ID                   AS city_id
     , ci.name                 AS city_name
     , ci.Country              AS city_country
     , ci.Population           AS city_population
     , co.LifeExpectancy       AS country_lifeexpectancy   
     , co.Name                 AS country_name
     , co.SurfaceArea          AS country_surfacearea
     , co.GNP                  AS country_gnp
     , ROUND(100 * ci.Population / co.Population, 1) AS `CityPopulationPercentageOfCountryPopulation`
  FROM City ci
  JOIN Country co
    ON co.ID = ci.Country_ID
 WHERE co.SurfaceArea BETWEEN 3000000 AND 40000000
   AND CHAR_LENGTH(ci.District) > 4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM