MySQL,连接两列 [英] MySQL, Concatenate two columns

查看:25
本文介绍了MySQL,连接两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MySQL 表中有两列:SUBJECTYEAR.

There are two columns in a MySQL table: SUBJECT and YEAR.

我想生成一个字母数字唯一编号,其中包含来自 SUBJECT 和 YEAR 的串联数据.

I want to generate an alphanumeric unique number which holds the concatenated data from SUBJECT and YEAR.

我该怎么做?是否可以使用像 + 这样的简单运算符?

How can I do this? Is it possible to use a simple operator like +?

推荐答案

您可以使用 CONCAT 函数如下:

You can use the CONCAT function like this:

SELECT CONCAT(`SUBJECT`, ' ', `YEAR`) FROM `table`

更新:

要获得该结果,您可以尝试以下操作:

To get that result you can try this:

SET @rn := 0;

SELECT CONCAT(`SUBJECT`,'-',`YEAR`,'-',LPAD(@rn := @rn+1,3,'0'))
FROM `table`

这篇关于MySQL,连接两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆