Posts in category “Tips”

MySQL: Delete All Duplicate Rows Except the Earliest One in One SQL

You want to add a unique index to a table, and unfortunately, there are already many duplicate rows in it. Manually find and delete these rows is time-wasting and error-prone. So why won't we just write one SQL statement and quickly resolve it?

First try, I wrote the following statement, and it won't work:

DELETE FROM PromotionSkus A 
WHERE 
	A.SkuId IN (SELECT SkuId FROM PromotionSkus B GROUP BY B.SkuId HAVING COUNT(B.SkuId) > 1) 
	AND 
	A.Id NOT IN (SELECT MIN(Id) FROM PromotionSkus C GROUP BY C.SkuId HAVING COUNT(C.SkuId) > 1);

AND this one below works!

DELETE FROM PromotionSkus A
WHERE 
	A.Id NOT IN (SELECT Id FROM (SELECT MIN(Id) AS Id, COUNT(SkuId) AS Total FROM PromotionSkus GROUP BY SkuId HAVING Total > 1) AS B)
	AND 
	A.SkuId IN (SELECT SkuId FROM (SELECT SkuId FROM PromotionSkus GROUP BY SkuId HAVING COUNT(SkuId) > 1) AS C);

The reason is well explained in this brilliant article.

Another mysql tip: using mysqldump export a table with one line one row.

mysqldump --databases YourDataBaseName --tables YourTableName --skip-extended-insert

Why do we need that? It is much easier to compare !

Using the cookie feature in Insomnia Core

It took me more than an hour to understand how to use cookies when you are sending a request to the API server.

The most important thing is that you only need to correctly add the cookies, then the cookies will be automatically sent to the API server. Don't try to add a cookie item at the headers' configuration part!

vim 命令模式粘贴寄存器中复制好的内容:先Ctrl+R,再输入寄存器名字。默认寄存器名字是 "

vim 命令模式粘贴寄存器中复制好的内容:先Ctrl+R,再输入寄存器名字。默认寄存器名字是 "

Jenkins 三言两语(二)

  1. Jenkins 虽然通过ssh访问一个node来执行build或者 deploy任务,而且它也确实执行了远程机器的.bashrc 脚本,但它却刻意的抹掉了PATH 环境变量。所以在你要运行的bash脚本里再 source 一遍 /etc/profile 就有了必要。
  2. Jenkins自己的环境变量,可以在脚本里直接用,但从bash里来的环境变量,如 $?,就得用反斜线转义 \$ 符号。
  3. .bashrc 别 echo 东西,否则 Jenkins 会拒绝干活。(应该弄一台专门的机器只跑 Jenkins)

PS: 上面第2条其实描述不准确,后面踩的坑证明,如果用 sh 命令里有环境变量,老老实实用单引号,如果非用双引号,你反而要用\$转义。切记!Jenkins环境不是bash环境。不论是否 Jenkins里定义的环境变量,如果要在bash脚本里用,就老老实实用单引号。

vim 向上删除 n 行

我们都知道从当前行往下删除 n 行很容易, ndd 就好了。但往上删除呢? 我以前是不知道的,最近搜索了一下,找到了答案,而且额外学到了其他的技巧,你想听吗(不想听我也会写,哈哈

8dd其实是dd重复8次,容易记忆,但效率很低。正确的姿势是 7dj 或者 d7j。我想你看到这里已经猜到应该怎么向上删除了。如果打算从当前行往上删除n行,你需要 {n-1}dk 或者 d{n-1}k

故事到这里应该结束了,然而.... 试试 V3kd, V4jd,注意是大写的V。你会对vim的强大有更多的理解。

By the way, 如果你要从当前行删除到你知道行号的某一行,举例来说,你要从当前行一直删到第434行,无所谓是向上删还是向下删,用 d434G 就可以了。