我在表格中有三列。
要求:col2和col3的值应为col1。
下面显示了我现在的表格,需要进行更改。
col1 col2 col3
AB football
AB football
ER driving
ER driving
TR city
TR city
下面显示了需要更改为
的表格col1 col2 col3
AB_football_1 AB football
AB_football_2 AB football
ER_driving_1 ER driving
ER_driving_2 ER driving
TR_city_1 TR city
TR_city_2 TR city
正如您在col1中看到的那样,它应该采用col2,put(下划线),然后是col3,put(下划线)然后根据col2和col3中的值递增数字。
这可以在CREATE或SELECT或INSERT语句或Trigger语句中进行,如果有的话,任何提示都会感激不尽。
答案 0 :(得分:1)
使用SELECT
窗口函数很容易做到(row_number()
),如下所示:
select
col2 ||'_'|| col3 ||'_'|| row_number() over(partition by col2, col3 order by col2) as col1,
col2,
col3
from t
答案 1 :(得分:1)
尝试
SELECT col2
||'_'
||col3
||'_'
||rank col1,
col2,
col3
FROM (SELECT col2,
col3,
ROW_NUMBER()
OVER(
PARTITION BY col2, col3
ORDER BY col2) rank
FROM my_table)
<强>输出强>
+---------------+------+----------+
| COL1 | COL2 | COL3 |
+---------------+------+----------+
| AB_football_1 | AB | football |
| AB_football_2 | AB | football |
| ER_driving _1 | ER | driving |
| ER_driving _2 | ER | driving |
| TR_city _1 | TR | city |
| TR_city _2 | TR | city |
+---------------+------+----------+
答案 2 :(得分:0)
/* table is */
col1 col2 col3
test 123
/* Try this query */
UPDATE `demo`
SET `col1` = concat(col2, '_', col3)
/* Output will be */
col1 col2 col3
test_123 test 123