LoginSignup
2
2

More than 5 years have passed since last update.

SQLの結合条件の右辺と左辺の記述順を統一するワンライナー

Posted at
select * 
  from foo f
  join bar b on f.id = b.foo_id 

になっているものを

select * 
  from foo f
  join bar b on b.foo_id = f.id

のように、直近のテーブルエイリアスbをイコールの左辺側に持ってくるワンライナー

perl -pi -e 's/([^\.\s=]+) on ([^\.\s=]+)(\.[^\.\s=]+) = ([^\.\s=]+)(\.[^\.\s=]+)/$1 on $4$5 = $2$3/ if /([^\.\s=]+) on ([^\.\s=]+)(\.[^\.\s=]+) = ([^\.\s=]+)(\.[^\.\s=]+)/ and $1 ne $2' **/*.sql

テーブル結合条件のイコールの右辺と左辺の記述順は、
順序的にどちらがいいのかは正直微妙ですが、
これを統一してdiff等で余計な差分を出させないのが目的です。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2