[英]how auto concat string length in mysql select
ex. c1, c2 = > result
c1 c1 result
1 1 to 1000001
2 9 to 2000009
3 1 to 3000001
21 34 to 2100034
22 35 to 2200035
23 55 to 2300055
111 1234 to 1111234
112 8392 to 1128392
113 2833 to 1132833
a part of my MySQL SELECT CONCAT() statement with cut out "c1" look like,
IF(CHAR_LENGTH(`c2`)=1, concat('00000',`c2`),
IF(CHAR_LENGTH(`c2`)=2, concat('0000',`c2`),
IF(CHAR_LENGTH(`c2`)=3, concat('000',`c2`),
IF(CHAR_LENGTH(`c2`)=4, concat('00',`c2`),
IF(CHAR_LENGTH(`c2`)=5, concat('0',`c2`),`c2` )))))
But is there any other way to reduce this code for concat c1 with c1 into result with zero at the middle and with auto calculation of how many zeros have to be added? 但是,还有其他方法可以将concat c1和c1的代码减少为中间为零的结果,并自动计算必须添加多少个零吗?
看看LPAD 。
There might be a math function to help you here, although I don' t know if it is quicker. 可能有一个数学函数来帮助你,虽然我不知道它是否更快。
Lets see. 让我们来看看。 you want to multiply a single value for c1 by 1000000 and then add c2.
您想将c1的单个值乘以1000000,然后加c2。 For the more general case, you want to multiply by
对于更一般的情况,您想要乘以
1000000 / 10^(char_length(`c1`)-1)
more: Your final value will be 更多:您的最终价值将是
(c1 * (1000000 / 10^(char_length(`c1`)-1))) + c2
But that LPAD function from @eumiro might be a better answer 但是@eumiro的LPAD函数可能是一个更好的答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.