如何选择身高和体重增长的人?

时间:2017-05-02 09:31:58

标签: mysql sql

我有3个表,如下所示。人,体重和身高 我如何选择自2001年至2004年(特定年份范围)的体重和身高增长的人?(约翰每年在这两个参数上增长20%)

预期的查询结果是“John”

enter image description here

4 个答案:

答案 0 :(得分:0)


您可以使用以下查询,

    SELECT P.NAME,W.YEAR,W.WEIGHT,H.HEIGHT FROM PERSON P,
    WEIGHT W,
    HEIGHT H,
    (SELECT MIN(WEIGHT) AS WEIGHT1 FROM WEIGHT WHERE YEAR = 2001) W1,
    (SELECT MIN(HEIGHT) AS HEIGHT1 FROM HEIGHT WHERE YEAR = 2001) H1
    WHERE 
    P.ID = W.FK_PERSON AND
    P.ID = H.FK_PERSON AND
    W.WEIGHT >= W1.WEIGHT1 AND
    H.HEIGHT >= H1.HEIGHT1 

您也可以使用连接,

SELECT P.NAME,W.YEAR,W.WEIGHT,H.HEIGHT FROM PERSON P
INNER JOIN WEIGHT W
ON (P.ID = W.FK_PERSON)
WEIGHT W
INNER JOIN HEIGHT H
ON (P.ID = H.FK_PERSON)
HEIGHT H
INNER JOIN
(SELECT MIN(WEIGHT) AS WEIGHT1 FROM WEIGHT WHERE YEAR = 2001) W1
ON (P.ID = W1.FK_PERSON)
INNER JOIN
(SELECT MIN(HEIGHT) AS HEIGHT1 FROM HEIGHT WHERE YEAR = 2001) H1
ON (P.ID = H.FK_PERSON)
WHERE 
W.WEIGHT >= W1.WEIGHT1 AND
H.HEIGHT >= H1.HEIGHT1

如果您遇到任何问题,请告诉我

答案 1 :(得分:0)

您需要做一些事情:

select h1.fk_person, h1.height as oldHeight, h1.year as FirstYear, h2.height as NewHeight, h2.year as LastYear, ((h2.height-h1.height)/(h2.year-h1.year))/h1.height as pc_growth_py
from height h1
inner join height h2
  on h1.fk_person = h2.fk_person
where h1.year = (select max(year) from height x1 where x1.person = h1.person)
and h2.year = (select max(year) from height x2 where x2.person = h2.person)

重复体重......

然后这样做:

select Name
from Person p1
inner join <Height query> a1
on a1.fk_person = p1.id
inner join <weight query> a2
on a2.fk_person = p1.id
order by a1.pc_growth_py desc, a2.pc_growth_py desc
limit 1

答案 2 :(得分:0)

这将为您提供身高和体重每年仅增加的人:

select p.ID, p.Name
from (
    /* Tells you if the person did not gain weight since the previous year */
    select w.FK_Person, w.Year, case when w1.Weight > w.Weight or w1.Weight is null then 'gain' else 'not gain' end gain
    from Weight w
    left outer join Weight w1 on w.FK_Person = w1.FK_Person and w1.Year = w.year + 1    
) w
join (
    /* Tells you if the person did not gain height since the previous year */
    select w.FK_Person, w.Year, case when w1.Height > w.Height or w1.Height is null then 'gain' else 'not gain' end gain
    from Height w
    left outer join Height w1 on w.FK_Person = w1.FK_Person and w1.Year = w.year + 1    
) h on w.FK_Person = h.FK_Person and w.Year = h.Year
join Person p on p.ID = w.FK_Person
group by p.ID, p.Name
/* Filter out rows where the person had at least one year of not gaining */
having sum(case when w.gain = 'not gain' or h.gain = 'not gain' then 1 else 0 end) = 0

答案 3 :(得分:-2)

以下是您可以参考的示例,它可能有所帮助。

mysql -u root -ptest -D my_db -h 127.0.0.1

你明白了......

然后,您的用户表将引用高度权重表 - 可能还有许多其他维度表 - 占星符号,婚姻状况等。

   There are several ways... one is to just have two numeric columns, one for height, one for weight, then do the conversions (if necessary) at display time.  Another is to create a "height" table and a "weight" table, each with a primary key that is linked from another table.  Then you can store both English and metric values in these tables (along with any other meta info you want):

CREATE TABLE height (
    id          SERIAL PRIMARY KEY,
    english     VARCHAR,
    inches      INT,
    cm          INT,
    hands       INT  // As in, the height of a horse
);

INSERT INTO height VALUES
    (1,'4 feet',           48, 122, 12),
    (2,'4 feet, 1 inch',   49, 124, 12),
    (3,'4 feet, 2 inches', 50, 127, 12),
    (3,'4 feet, 3 inches', 51, 130, 12),
    ....

然后搜索4到5英尺之间的用户:

CREATE TABLE users (
    uid         SERIAL PRIMARY KEY,
    height      INT REFERENCES height(id),
    weight      INT references weight(id),
    sign        INT references sign(id),
    ...
);

这种方法有几个优点:

  • 你不必复制“努力”(好像是任何实际工作)来进行显示转换 - 只需选择你想要显示的格式!
  • 它使HTML选择中的下拉框变得非常容易 - 例如SELECT * FROM users JOIN height ON users.height = height.id WHERE height.inches >= 48 AND height.inches <= 60;
  • 它使您的各种维度的逻辑 - 包括非数字维度(如占星标志)明显相似 - 您没有为每种数据类型提供特殊的案例代码。
  • 它很好地扩展
  • 可以轻松添加数据的新表示形式(例如,将“指针”列添加到高度表中)