简体   繁体   English

SQL:为嵌套 SELECT 创建全局别名以在另一个嵌套 SELECT 中使用

[英]SQL: create global alias for nested SELECT to be used in another nested SELECT

Let's assume I've got a database with two tables: people which contains person's id and his/her birth year for each person and parents which contains the (parent_id, child_id) pairs to represent the relative relationships between people.假设我有一个包含两个表的数据库: people包含每个人的 id 和他/她的出生年份,以及包含(parent_id, child_id)对的parents ,以表示人与人之间的相对关系。 To make the explanation easier let's assume each person has either 0 children or 1 child.为了使解释更容易,让我们假设每个人有 0 个孩子或 1 个孩子。 Here is an example of the data in the database (as a set of SQL statements to create it on MySQL):以下是数据库中数据的示例(作为在 MySQL 上创建它的一组 SQL 语句):

CREATE TABLE people (
    id  INTEGER NOT NULL AUTO_INCREMENT,
    birth_year  INTEGER NOT NULL,
    CONSTRAINT PRIMARY KEY(id)
);

CREATE TABLE parents (
    parent_id   INTEGER NOT NULL,
    child_id    INTEGER NOT NULL,
    CONSTRAINT PRIMARY KEY(parent_id,child_id)
);

-- Not creating FOREIGN KEYS, because it's just an example

INSERT INTO people (id, birth_year) VALUES (1, 1937);
INSERT INTO people (id, birth_year) VALUES (2, 1943);
INSERT INTO people (id, birth_year) VALUES (3, 1974);
INSERT INTO people (id, birth_year) VALUES (4, 2001);
INSERT INTO people (id, birth_year) VALUES (5, 2020);

INSERT INTO parents (parent_id, child_id) VALUES (1, 4);
INSERT INTO parents (parent_id, child_id) VALUES (3, 5);

Result:结果:

人桌

父母桌

Now I want to make up a query which will retrieve the id of the person, whose child was born at the earliest age of the parent (for example, if I was born in 1234 and my child was born in 1300, then my age when my child was born was 1300 - 1234 = 66 and I would like to find a person which got their child earlier than others).现在我想编写一个查询来检索那个人的id ,他的孩子出生于父母的最早年龄(例如,如果我出生于 1234 年,而我的孩子出生于 1300 年,那么我的年龄是什么时候)我的孩子出生是1300 - 1234 = 66 ,我想找一个比别人早生孩子的人)。

I have made up some queries for it, but each of them either didn't work or had duplications or both.我已经为它做了一些查询,但它们中的每一个要么不起作用,要么有重复,或两者兼而有之。 The one I like most is我最喜欢的是

SELECT id AS pid, -- Parent id
(SELECT child_id FROM parents WHERE parent_id=pid) AS cid -- Child id
FROM people WHERE
EXISTS(SELECT cid) -- Only selecting parents who have children (not sure this part is correct)
ORDER BY (birth_year - (SELECT birth_year FROM people WHERE id=cid)) ASC -- Order by age when they got their child
LIMIT 1;

But this one fails in MySQL with the error:但是这个在 MySQL 中失败并出现错误:

ERROR 1054 (42S22) at line 24: Unknown column 'cid' in 'field list'

How do I fix the error?如何修复错误? Another thing I am worried about is that as a result, I will select not only the parent's id but also the id of one of his/her children.我担心的另一件事是,结果,我不仅会选择父母的 ID,还会选择他/她的一个孩子的 ID。 Is it possible to avoid it?有可能避免吗? Probably there is a better way to select the data I'm looking for?可能有更好的方法来选择我正在寻找的数据?

You can get the ages by using joins:您可以使用连接获取年龄:

select c.*, (p.birth_year - c.birth_year) as parent_age 
from parents pa join
     people p
     on pa.parent_id = p.id join
     people c
     on pa.child_id = pc.id;

To get all the rows with the minimum, use window functions:要获取所有行的最小值,请使用窗口函数:

select x.*
from (select c.*, (p.birth_year - c.birth_year) as parent_age,
             min(p.birth_year - c.birth_year) over () as min_age
      from parents pa join
           people p
           on pa.parent_id = p.id join
           people c
           on pa.child_id = pc.id
     ) x
where parent_age = min_age;

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

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