编写SQL语句时出现问题

时间:2011-08-03 21:46:15

标签: mysql sql datediff jointable

我有一个名为Hotel的MySQL表。我有以下字段IDhotelNameAddressYearOfEstablishment

YearOfEstablishmentDate类型,它会将日期存储为2010-12-26。我需要做的是找到日期之间的差异,其中的SQL如下;

SELECT DATEDIFF('2011-08-08','2010-07-26');

以上查询的答案将在几天内给出,例如两个日期之间的差异可能是120天,我需要在Years中显示。喜欢将值120除以365并给出年数。我如何为此编写SQL语句?

2。)现在我需要编写另一个SQL,它将显示所有Hotel表列和Year difference values(如上所述)。我如何为此编写SQL?

(我正在使用MySQL)

3 个答案:

答案 0 :(得分:1)

  1. SELECT DATEDIFF('2011-08-08','2010-07-26') / 365;
  2. SELECT ID,hotelName,Address,YearOfEstablishment, DATEDIFF(YearOfEstablishment,'2010-07-26') / 365 AS years FROM Hotel;
  3. 但是,请务必知道average year has 365.2425 days

    如果您现在是参考日期时间,只需使用NOW()SELECT DATEDIFF('2011-08-08',NOW()) / 365;

答案 1 :(得分:1)

你应该能够使用这样的查询:

SELECT ID, hotelName, Address, YearOfEstablishment, (DATEDIFF(YearOfEstablishment, '2010-07-26')/365) AS differenceInYears

答案 2 :(得分:0)

试试这个

SELECT MOD (DATEDIFF('2011-08-08','2010-07-26'), 365) ;