php - Mysql查询CONCAT改变输出

标签 php mysql sql concatenation

我有以下查询:

mysql> select (select m.delay from messages m order by message_id desc limit 1) as Delay, s.min_delay, s.max_delay from statistics s order by s.date_hour desc limit 1;
+-------+-----------+-----------+
| Delay | min_delay | max_delay |
+-------+-----------+-----------+
|    10 |      7.00 |     12.00 |
+-------+-----------+-----------+
1 row in set (0.00 sec)

这很好。但我需要得到以下格式的结果:

 Delay=10 Min_Delay=7.00 Max_Delay=12.00

我可以使用 concat 来做到这一点吗?

最佳答案

通常,您会在应用层进行转换。但是,如果你想使用 concat(),你可以在 MySQL 中完成:

select concat_ws(' ',
                 concat('Delay=', (select m.delay from messages m order by message_id desc limit 1)),
                 concat('Min_Delay=', s.min_delay),
                 concat('Max_Delay=', s.max_delay)
                )
from statistics s
order by s.date_hour desc
limit 1

关于php - Mysql查询CONCAT改变输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28285026/

相关文章:

mysql - 未导入 pymysql 但出现 "pymysql.err.OperationalError: (2003, "无法连接到 MySQL 服务器”

php - 如何从字符串格式的列中获取最后插入的行值?

php - 交易错误

PHP fgetcsv 转义 '\'

php - 如何保存 portlet 位置

php - 如何添加前导零

mysql - 未知列的结构数据库

php - 以小背景显示文本

php - MySQL 存储过程没有返回插入 ID?

mysql:简化请求 - 按多字段分组