2

I have a MySQL database. In a couple of tables, the information that gets stored needs to be retrievable by week. So, I want to be able to do a SELECT FROM *database* WHERE week = *week*. The problem that I have is that the week part is stored as a unix timestamp (to allow for more versatilty like getting the date and time, just time, etc...).

So the question: How can I retrieve this record WHERE date = *date* when the stored date is a unix timestamp and date I'm matching it against is not?

If my question is too confusing and something needs to be rephrased or said in a clearer manner please comment and let me know.

4

1 回答 1

2

WEEK()MySQL 有一个用于处理日期的内置方法: MySQL WEEK() 参考

然而不幸的是,MySQL 的 WEEK() 方法只支持DATE数据类型,而不支持UNIX TIMESTAMP. 因此,我们必须首先将时间戳转换为日期,然后才能将该日期传递给 WEEK() 方法:

SELECT
  *
FROM
  my_table
WHERE
  WEEK(
    DATE_FORMAT(
      FROM_UNIXTIME('my_unix_timestamp_col'),
      '%e %b %Y'
    )
  ) = 51

如果您有一列是DATE数据类型,则可以简化查询(也可以使用索引):

SELECT * FROM my_table WHERE WEEK(my_date_col) = 51
于 2013-05-28T23:20:53.817 回答