在 Mysql 语句中使用 concat 的问题

Wonny 2009-12-18 10:46:41
VB前端,mysql 数据库。
select concat( month(frommonth),'月→',Cast(month(tomonth) AS CHAR),'月') as 征费时段 from mData

select concat( month(frommonth),'月→',month(tomonth) ,'月') as 征费时段 from mData
frommonth 和 tomonth 都是 DATE 字段。
结果 12月→1(第二个实际是 12月)
12月→6
请问如何解决这个问题?
谢谢。
...全文
163 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
YHL27 2009-12-22
  • 打赏
  • 举报
回复
study!
ACMAIN_CHM 2009-12-19
  • 打赏
  • 举报
回复
知道你的问题估计是什么了,你根本没有设置字符集! 在你的程序中先执行一下 "set names 'gkb'"

mysql> select concat( month(current_date()),'月→',Cast(month(current_date()) AS
CHAR),'月');
+-------------------------------------------------------------------------------
-+
| concat( month(current_date()),'月→',Cast(month(current_date()) AS CHAR),'月')
|
+-------------------------------------------------------------------------------
-+
| 12月→12月
|
+-------------------------------------------------------------------------------
-+
1 row in set (0.00 sec)

mysql> select concat( month(current_date()),'月→',month(current_date()) ,'月');

+-------------------------------------------------------------------+
| concat( month(current_date()),'月→',month(current_date()) ,'月') |
+-------------------------------------------------------------------+
| 12月→12月 |
+-------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>
平凡的思想者 2009-12-19
  • 打赏
  • 举报
回复
正解。

[Quote=引用 1 楼 acmain_chm 的回复:]
mysql 中根本不需要用cast .

select concat( month(frommonth),'月→',month(tomonth) ,'月') as 征费时段 from mData

应该就可以了。

mysql> select concat( month(current_date()),'YY',month(current_date()) ,'YY');
+-----------------------------------------------------------------+
| concat( month(current_date()),'YY',month(current_date()) ,'YY') |
+-----------------------------------------------------------------+
| 12YY12YY                                                        |
+-----------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>
[/Quote]
ACMAIN_CHM 2009-12-18
  • 打赏
  • 举报
回复
mysql 中根本不需要用cast .

select concat( month(frommonth),'月→',month(tomonth) ,'月') as 征费时段 from mData

应该就可以了。

mysql> select concat( month(current_date()),'YY',month(current_date()) ,'YY');
+-----------------------------------------------------------------+
| concat( month(current_date()),'YY',month(current_date()) ,'YY') |
+-----------------------------------------------------------------+
| 12YY12YY |
+-----------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>

56,940

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧