Unix 时间 & mysql

发布于 2024-08-01 22:33:56 字数 312 浏览 9 评论 0原文

我的表上有一个名为 X 的字段,我存储 unixtime(php -> time())。 我的问题是:

如何列出数据库中的所有月份,例如:6.2009 8.2009 等..

我的第二个问题是:

如何进行查询以列出同一字段 X 上的月份和年份的所有信息(我存储 unixtime),我知道这不起作用,但也许您可以更好地理解它:where 日期 = '06.2009'

有解决方案吗?

I have a field on my table which is called X, and I store unixtime(php -> time()).
My question is:

How can I list all the months from my DB, something like: 6.2009 8.2009 etc..

And my second question is:

How can I make a query to list all the informations based on a month and year on the same field X(i store unixtime), I know this doesn't work, but mabe you can understand it better: where date = '06.2009'

Any solutions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倒数 2024-08-08 22:33:56
  • 列出所有月份:

    从 MyTable 中选择 DISTINCT EXTRACT(YEAR_MONTH FROM FROM_UNIXTIME(X)); 
      
  • 返回给定年份和年份的所有行。 月份:

    从 MyTable 中选择 * 
      WHERE EXTRACT(YEAR_MONTH FROM FROM_UNIXTIME(X)) = 200906; 
      
  • List all months:

    SELECT DISTINCT EXTRACT(YEAR_MONTH FROM FROM_UNIXTIME(X)) FROM MyTable;
    
  • Return all rows with a given year & month:

    SELECT * FROM MyTable
    WHERE EXTRACT(YEAR_MONTH FROM FROM_UNIXTIME(X)) = 200906;
    
兔姬 2024-08-08 22:33:56

试试这个

  1. SELECT FROM_UNIXTIME(X,'%m.%Y') FROM TABLE GROUP BY FROM_UNIXTIME(X,'%m.%Y')

  2. SELECT * FROM TABLE WHERE FROM_UNIXTIME(X,'%m.%Y')='06.2009'

再见

Try this

  1. SELECT FROM_UNIXTIME(X,'%m.%Y') FROM TABLE GROUP BY FROM_UNIXTIME(X,'%m.%Y')

  2. SELECT * FROM TABLE WHERE FROM_UNIXTIME(X,'%m.%Y')='06.2009'

Bye

沫离伤花 2024-08-08 22:33:56

要回答你的第一个问题,你可以通过分组来做到这一点:

SELECT FROM_UNIXTIME(timestamp_column, '%c.%Y') AS month, some_other_column FROM table_name GROUP BY month;

至于你的第二个问题,这取决于你想要做什么。 例如:

SELECT AVG(payment), SUM(*), FROM_UNIXTIME(timestamp_column, '%c.%Y') AS month, some_other_column FROM table_name WHERE timestamp_column BETWEEN UNIX_TIMESTAMP(200906) AND UNIX_TIMESTAMP(200907) - 1 GROUP BY month;

将返回每组的付款平均值和行数(总和)。

要获取特定时间范围内未分组的信息,请像这样减少查询:

SELECT payment, some_other_column FROM table_name WHERE timestamp_column BETWEEN UNIX_TIMESTAMP(200906) AND UNIX_TIMESTAMP(200907) - 1;

To answer your first question, you would do this with grouping:

SELECT FROM_UNIXTIME(timestamp_column, '%c.%Y') AS month, some_other_column FROM table_name GROUP BY month;

As for your second question, this depends on what you're trying to do. For example:

SELECT AVG(payment), SUM(*), FROM_UNIXTIME(timestamp_column, '%c.%Y') AS month, some_other_column FROM table_name WHERE timestamp_column BETWEEN UNIX_TIMESTAMP(200906) AND UNIX_TIMESTAMP(200907) - 1 GROUP BY month;

Would return the average of the payments and the number (sum) of rows for each group.

To get information ungrouped from a specific timeframe, reduce the query like so:

SELECT payment, some_other_column FROM table_name WHERE timestamp_column BETWEEN UNIX_TIMESTAMP(200906) AND UNIX_TIMESTAMP(200907) - 1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文