trying to get the number of months

前端 未结 1 607
天涯浪人
天涯浪人 2020-12-20 00:13

membership table

  • membership_startdate (2011-01-12)
  • membership_dueday values like only dates (09,08,07)
  • member_id

相关标签:
1条回答
  • 2020-12-20 00:44

    The 1054 error is because the column does not exist in the table(s) defined in the FROM clause. Additionally, the WHERE clause is not used to set a variable, or column alias -- it's for filtering rows returned.

    Use DATEDIFF:

    SELECT t.member_id,
           DATEDIFF(LEAST(NOW(), t.membership_dueday), t.membership_startdate) / 30
      FROM MEMBERSHIP t
    

    The LEAST function will return the lowest of the two dates, so it will use the current date if the due date is in the future. You can use the GREATEST function if you want that reversed.

    0 讨论(0)
提交回复
热议问题