【问题标题】:How to select from partition while concatenating its name in MySQL如何在 MySQL 中连接名称时从分区中进行选择
【发布时间】:2015-05-14 11:34:35
【问题描述】:

我也有同样的问题: how to select dynamically in select * from <table_name> partiton (Partition name)? 但在 Mysql 中。

使用时:

select concat('p', year(now()), month(now()));

回复是:

+----------------------------------------+
| concat('p', year(now()), month(now())) |
+----------------------------------------+
| p20153                                 |
+----------------------------------------+

尝试使用时:

select max(ttime) from table partition(select concat('p', year(now()), month(now()))));

我收到一个错误:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select concat('p', year(now()), month(now()))))' at line 1

当然,在尝试 select max(ttime) from table partition(p20153) 时会按预期工作:

root@localhost [tom]> select max(ttime) from table partition(p20153);
+---------------------+
| max(ttime)          |
+---------------------+
| 2015-03-16 09:54:34 |
+---------------------+
1 row in set (0.00 sec)

注意动态部分是分区名而不是表名。 样本数据:

root@localhost [tom]> select id, ttime, value from table partition(p20153) limit 10;
+----------+---------------------+----------+
| id       | ttime               | value    |
+----------+---------------------+----------+
| 13964275 | 2015-03-01 00:05:11 | 16450824 |
| 13964291 | 2015-03-01 00:08:12 | 15964352 |
| 13964332 | 2015-03-01 00:09:42 | 14701984 |
| 13964379 | 2015-03-01 00:13:27 |    26128 |
| 13964411 | 2015-03-01 00:16:29 | 11073744 |
| 13964452 | 2015-03-01 00:20:34 | 14747992 |
| 13964486 | 2015-03-01 00:23:35 |   177800 |
| 13964507 | 2015-03-01 00:26:36 | 16786408 |
| 13964542 | 2015-03-01 00:28:28 |  8749552 |
| 13964571 | 2015-03-01 00:31:30 | 16932344 |
+----------+---------------------+----------+
10 rows in set (0.00 sec)

【问题讨论】:

  • 请编辑您的问题并添加预期输出以及示例数据

标签: mysql sql select partition


【解决方案1】:

您在查询中使用的语法不正确,试试这个

select max(ttime) from 
(
  select concat('p', year(now()), month(now())) as ttime 
)
temp 

【讨论】:

  • 不确定此查询有何帮助,因为 ttime 将获取分区名称,我只选择一个值的最大值。我想我误导了你,我在问题中添加了示例数据和预期结果。
猜你喜欢
  • 2012-12-16
  • 1970-01-01
  • 2015-04-28
  • 2023-03-29
  • 1970-01-01
  • 2019-09-06
  • 2013-09-20
  • 2012-06-10
  • 1970-01-01
相关资源
最近更新 更多