简体   繁体   English

查询以下内容的最佳或最简单方法是什么

[英]What is the best or simplest way to query the following

I need help with this problem.我需要帮助解决这个问题。

Question: Write an SQL query that lists all the species found in the oldest zoo in America.问题:编写一个 SQL 查询,列出在美国最古老的动物园中发现的所有物种。

I tried the following, which worked, but I would like to know if there is a simpler way to do this.我尝试了以下方法,它有效,但我想知道是否有更简单的方法可以做到这一点。 I spent hours on this question.我在这个问题上花了几个小时。

In the first block I created a new table I called "usaT", using the INTO clause, which only hold zoos with country USA.在第一个块中,我使用 INTO 子句创建了一个名为“usaT”的新表,该表仅包含美国国家/地区的动物园。 Click data base link to see image, please.请点击数据库链接查看图片。 Then i query this "usaT" table in the 2nd block of code.然后我在第二个代码块中查询这个“usaT”表。

Thanks for the help谢谢您的帮助

database photo数据库照片

    SELECT * INTO usaT
    FROM zoos WHERE country='USA';

    SELECT species FROM animals, usaT
    WHERE year_founded = (SELECT MIN(year_founded) FROM usaT)
    AND animals.zoo=usaT.city;
  • Don't use the ANSI (pre-ISO) style of SQL JOINs (ie FROM x, y WHERE x.fk = y.fk ).不要使用 SQL JOIN 的 ANSI(前 ISO)样式(即FROM x, y WHERE x.fk = y.fk )。 Always use an explicit JOIN .始终使用显式JOIN

  • Use SELECT DISTINCT to prevent duplicate results where a zoo has more than 1 animal of the same species.使用SELECT DISTINCT可防止动物园拥有超过 1 只相同物种的动物时出现重复结果。

  • You can get the oldest zoo by doing SELECT TOP 1... ORDER BY (in MS SQL Server)您可以通过执行SELECT TOP 1... ORDER BY获得最古老的动物园(在 MS SQL 服务器中)

    • In PostgreSQL (which I think you're using) you use LIMIT .在 PostgreSQL (我认为您正在使用)中,您使用LIMIT

Like so:像这样:

MS SQL Server微软 SQL 服务器

SELECT
    DISTINCT
    animals.species
FROM
    animals
    INNER JOIN
    (
        SELECT
            TOP 1
            city
        FROM
            zoos
        WHERE
            country = 'USA'
        ORDER BY
            year_founded ASC
    ) AS oldest_zoo_in_usa ON
        animals.zoo = oldest_zoo_in_usa.city

PostgreSQL PostgreSQL

SELECT
    DISTINCT
    animals.species
FROM
    animals
    INNER JOIN
    (
        SELECT
            city
        FROM
            zoos
        WHERE
            country = 'USA'
        ORDER BY
            year_founded ASC
        LIMIT
            1
    ) AS oldest_zoo_in_usa ON
        animals.zoo = oldest_zoo_in_usa.city

Query would be:查询将是:

Select  s.*
from animals a join
zoos z on z.yearFounded=(Select min(year_founded) from zoos) and z.city=a.animals Join
Species s on a.species=s.species

You should assign the unique identification number to each row of the all table for simplicity and use that as primary key & foreign key relation for joins.为简单起见,您应该为 all 表的每一行分配唯一标识号,并将其用作连接的主键和外键关系。

select distinct species
 from animals a
 join zoos z 
   on z.year_founded in
    (select min(year_founded)
      from zoos
     where country = 'USA')
  and a.zoo = z.city;

Hope this helps.希望这可以帮助。

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

相关问题 删除字符串末尾子字符串的最简单/最佳方法是什么? - What is the simplest/best way to remove substrings at the end of a string? 以下递归查询的最佳可能实现是什么? - What is the best possible implementation for the following recursively query? 编码此相关查询的最佳方法是什么? (选择优先级后可能包含NULL的价格) - What is the best way to code this correlated query? (Select Prices that may contain NULL following a Priority) 给定以下数据,编写查询以生成表的最佳方法是什么? - What would be the best way to write a query to produce a table given the following data? 在SQL查询中包括COUNT个关联的最简单方法是什么? - What's the simplest way to include the COUNT of associations in a SQL query? 为JPA本机查询的结果分配ID的最简单方法是什么? - What is the simplest way to assign an ID to a result from a JPA native query? 进行 SQL 查询以生成累积订阅者的最简单方法是什么? - What's the simplest way to make a SQL query to generate cumulative subscribers? 重写此查询的最佳方法是什么 - What is the best way to rewrite this query 查询这些表的最佳方法是什么 - What is the best way to query these tables 什么是最好的和最简单的 SQL 查询以获得最好的 3 个分数及其相应的详细信息。 - What is the best and simplest SQL Query to get the Best 3 scores and their corresponding details.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM