简体   繁体   English

没有在 mysql 中修剪的 LPAD

[英]LPAD without trimming in mysql

I am trying to find a way to add 0 at the beginning of an ID without trimming it :我试图找到一种在 ID 开头添加 0 而不修剪它的方法:

I am currently using LPAD(id,4,"0") witch works fine until 9999我目前正在使用LPAD(id,4,"0")女巫工作正常,直到9999

  • Request : SELECT LPAD(12345,4,"0");请求: SELECT LPAD(12345,4,"0");
  • Excepted result : 12345异常结果: 12345
  • Result : 1234结果: 1234

I am Looking for a Request who does LPAD(id, MAX(LENGTH(id),4), "0")我正在寻找执行LPAD(id, MAX(LENGTH(id),4), "0")的请求

I found SELECT IF(LENGTH(12345)>4, 12345, LPAD(12345, 4, "0"));我发现SELECT IF(LENGTH(12345)>4, 12345, LPAD(12345, 4, "0")); but i would prefer if the 4 was in a single place (to make it easier use it elsewhere).但我更喜欢如果4在一个地方(以便在其他地方更容易使用它)。

  • Is there a build-in function that does what i'm looking For ?是否有内置功能可以满足我的需求?
  • Or is there an alternative to the function MAX() that would work in that situation ?或者有没有替代函数MAX()可以在这种情况下工作?
  • Or should I stick with the IF(LENGTH) solution and it's drawbacks ?或者我应该坚持使用IF(LENGTH)解决方案,它的缺点是什么?

Edit :编辑 :

ZEROFILL doesn't fit my needs because I also need the id without 0 s. ZEROFILL不符合我的需求,因为我还需要没有0的 id。 I mainly use the ID without the LPAD() , but when I do, I use it with a prefix : CONCAT("PFX", LPAD(id,4,"0"))我主要使用没有LPAD()的 ID,但是当我这样做时,我使用它的前缀: CONCAT("PFX", LPAD(id,4,"0"))

Thanks for your Help感谢您的帮助

PS: please tell me if i do anything wrong, it's my first time asking here. PS:如果我做错了什么,请告诉我,这是我第一次在这里提问。

You can cat the length of a int field and also set zerofill like this:您可以对 int 字段的长度进行分类,也可以像这样设置 zerofill:

CREATE TABLE `num` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `Numbers` int(5) unsigned zerofill DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Insert values:插入值:

INSERT INTO `num` (`id`, `Numbers`)
VALUES
    (1, 1),
    (2, 22),
    (3, 123456);

See the result:查看结果:

MariaDB []> select * from num;
+----+---------+
| id | Numbers |
+----+---------+
|  1 |   00001 |
|  2 |   00022 |
|  3 |  123456 |
+----+---------+
3 rows in set (0.000 sec)

MariaDB []>

without zerofill you can calc it like:没有 zerofill 你可以像这样计算它:

SELECT 
  id,
  SUBSTRING( CONCAT('000000',Numbers),- GREATEST(LENGTH(Numbers),5))
FROM num;

Result:结果:

MariaDB []>     SELECT
    ->       id,
    ->       SUBSTRING( CONCAT('000000',Numbers),- GREATEST(LENGTH(Numbers),5))
    ->     FROM num;
+----+--------------------------------------------------------------------+
| id | SUBSTRING( CONCAT('000000',Numbers),- GREATEST(LENGTH(Numbers),5)) |
+----+--------------------------------------------------------------------+
|  1 | 00001                                                              |
|  2 | 00022                                                              |
|  3 | 123456                                                             |
+----+--------------------------------------------------------------------+
3 rows in set (0.001 sec)

MariaDB []>

Well I had similar problem with LPAD, it was truncating number to its pad length.好吧,我在 LPAD 上遇到了类似的问题,它正在将数字截断为其焊盘长度。 According to https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_lpad it is expected result.根据https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_lpad这是预期的结果。

As far as I can see, nobody mentioned answer that solved my same problem:据我所知,没有人提到解决我同样问题的答案:

LPAD(id, GREATEST(LENGTH(id), 4), "0")

It works as expected.它按预期工作。 Pads any id 's shorter than 4 characters with 0, and returns unchanged id 's that are longer than 4 characters.垫的任何id的短于4个字符与0,并且返回不变id的是比4个字符。

I'm leaving my answer here for other people, that will find this question in the future.我将我的答案留给其他人,他们将来会发现这个问题。

Ask yourself, why a number would need leading zeroes at all.问问自己,为什么一个数字需要前导零。 No number in practical and theoretical math needs leading zeroes.实践和理论数学中的任何数字都不需要前导零。

Maybe you want to visuals something, and not work with the result as a real number.也许您想可视化某些东西,而不是将结果作为实数处理。 In that case, you can either declare the field in the table ZEROFILL and select it converted to char: SELECT CONVERT(id, CHAR);在这种情况下,您可以在表ZEROFILL声明该字段并选择它转换为字符: SELECT CONVERT(id, CHAR); . .

Other than that, the only way is a mix of CONCAT() and LENGTH() , that way you avoid stripping the ID to the value in LPAD() .除此之外,唯一的方法是混合使用CONCAT()LENGTH() ,这样您就可以避免将 ID 剥离为LPAD()的值。

LPAD LPAD

You can achieve this with an REGEXP_REPLACE to do a LPAD equivalent but without truncating the result.您可以使用REGEXP_REPLACE来实现这一点,以执行LPAD等效但不截断结果。 If you want a minimum length of 4 and padding with 0, you can do:如果您想要最小长度为 4 并用 0 填充,您可以执行以下操作:

REGEXP_REPLACE(CONCAT('0000', id), '^0{0,4}(.{4,})$', '\\1')

You can adapt this by replacing '0000' with REPEAT('0', 4) to do it programmatically.您可以通过将'0000'替换为REPEAT('0', 4)以编程方式进行调整。 The main advantage in this approach, is to not repeating the padded thing.这种方法的主要优点是不重复填充的东西。 In my use cases, I can have very long and complex things that I don't want to duplicate to only pad them, so this code fragment do its job.在我的用例中,我可以有很长很复杂的东西,我不想复制它们来只填充它们,所以这个代码片段可以完成它的工作。

RPAD遥控驾驶室

For those who have the problem for RPAD , you need a very close pattern:对于RPAD有问题的人,您需要一个非常接近的模式:

REGEXP_REPLACE(CONCAT(id, '0000'), '^(.{4,}?)0{0,4}$', '\\1')

The two blocks in the pattern are essentially inverted but you can see .{4,}?模式中的两个块基本上是倒置的,但您可以看到.{4,}? instead of .{4,} .而不是.{4,} It's for set the greediness to lazy for the {4,} quantifier.它用于将{4,}量词的贪婪设置为惰性。 It's needed here.这里需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM