110
92

More than 5 years have passed since last update.

備忘録:SQLのINを複数列指定で使う

Last updated at Posted at 2015-01-10

1:1 (列の数:条件の数)

列名=値で条件を指定
特定の値を持つ行を検索したい場合

select
    *
from
    table t
where
    t.col = "data";

普通。

1:n

列名 in (値...)
複数の値に一致する行を検索したい場合

select
    *
from
    table t
where
    t.col in ("data1", "data2");

あるいはorでつなげる。
割りと普通。
※INの中身が多くてSQLが長くてだめとかいうやつもいる

n:n

(列名, 列名, ...) in (値, 値, ... )

select
    *
from
    table t
where
    (t.col1, t.col2) in (
        ("hoge", "fuga"),
        ("foo", "bar")
    )

あんま必要になる場面がないから記憶の砂漠に埋まる。

その他

結合条件はfrom句に、検索条件はwhere句に書くのが個人的に好きです。「fromに検索条件書いてもいいやろ!!」という反論は受け付けた後にスルーします。

どうでもいいですが、テーブル名の別名をつけるのに毎回悩むのですが皆様はどうやってつけてるんですかね?。

110
92
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
110
92