简体   繁体   English

关于 CONCAT 和 DATE 以及 Have 子句 SQL 的问题

[英]Question about CONCAT and DATE and Have Clause SQL

I practicing writing SQL queries, I'm working with a baseball database.我练习编写 SQL 查询,我正在使用棒球数据库。 The table I'm concerned with is named people with attributes nameFist, nameLast, debute_date, and final_game.我关心的表是具有 nameFist、nameLast、debute_date 和 final_game 属性的命名人员。

I'm attempting to find the first name and last name (as one field) and debut date of people whose final game was 10,000 days after their debut.我试图找到他们的最后一场比赛是在他们首次亮相后 10,000 天的人的名字和姓氏(作为一个字段)和首次亮相日期。 Order by the date difference.按日期差异排序。

So far I have:到目前为止,我有:

SELECT CONCAT(nameFirst,'',nameLast) as name, debut_date FROM PEOPLE;

How would I find the people whose final game was 10,000 days after their debut?出道10000天后的最后一局怎么找人? debute_date and final_game is formatted like so: Year-Month-Day ei 2014-02-28.首次亮相日期和最终游戏的格式如下:Year-Month-Day ei 2014-02-28。

I can't just simply add 10,000 days to final_game.我不能简单地将 10,000 天添加到 final_game。

You can try using the DATEDIFF function.您可以尝试使用 DATEDIFF function。 So the query would look something like this:所以查询看起来像这样:

SELECT CONCAT(nameFirst,' ',nameLast) as name, debut_date
FROM PEOPLE
WHERE DATEDIFF(final_game, debut_date) > 10000
ORDER BY DATEDIFF(final_game, debut_date);

The DATEDIFF function may vary depending on if you are using MySQL or SQL SERVER. DATEDIFF function 可能会有所不同,具体取决于您使用的是 MySQL 还是 SQL 服务器。

For SQL SERVER, the query might look like this:对于 SQL SERVER,查询可能如下所示:

SELECT CONCAT(nameFirst,' ',nameLast) as name, debut_date
FROM PEOPLE
WHERE DATEDIFF(d, debut_date, final_game) > 10000
ORDER BY DATEDIFF(d, debut_date, final_game);

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

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