03.08
SQLのwhere 1=1 について。
「select * from table」
「select * from table where 条件1 」
「select * from table where 条件1 and 条件2 」
↓は↑と同じ結果を返す。
「select * from table where 1=1」
「select * from table where 1=1 and 条件1 」
「select * from table where 1=1 and 条件1 and 条件2 」
where 1=1を使うことで、動的にwhere句を作る際に入れたい条件を「and 条件」で後ろにしていけば良くなるのである!条件がなければ何もしなければいいだけなので、そもそもwhereいる?いらない?といったややこしいことに頭を使わなくて済む。