はじめに

MySQL で良く使うもの、便利なもの(、すぐに忘れるもの)などを忘れないようにメモ。
(何かあれば随時追記)

この記事が参考になった方
ここここからチャージや購入してくれると嬉しいです(ブログ主へのプレゼントではなく、ご自身へのチャージ)
欲しいもの / Wish list

目次

  1. はじめに
  2. 環境・条件
  3. 詳細
    1. 全テーブルからカラムの有無を検索
  4. 参考文献

環境・条件

1
2
3
4
5
6
7
$ sw_vers
ProductName: Mac OS X
ProductVersion: 10.15.6
BuildVersion: 19G2021

$ mysqld --version
mysqld Ver 5.7.31 for osx10.15 on x86_64 (Homebrew)

※随時追記なので、セクションごとに確認環境は変わる可能性あり

詳細

全テーブルからカラムの有無を検索

参考: MySQLで指定されたカラム名を持つテーブルを検索する - Qiita

1
select table_name, column_name from information_schema.columns where column_name = '検索対象カラム' and table_schema = '検索対象データベース';

実行イメージ

1
2
3
4
5
6
7
8
9
mysql> select table_name, column_name from information_schema.columns where column_name = 'user_id' and table_schema = 'my_db';
+------------+-------------+
| table_name | column_name |
+------------+-------------+
| followers | user_id |
| friends | user_id |
| ... | user_id |
+------------+-------------+
xx rows in set (0.02 sec)

LIKE と組合せてあいまい検索もできる。

1
2
3
4
5
6
7
8
9
10
mysql> select table_name, column_name from information_schema.columns where column_name LIKE '%_id' and table_schema = 'my_db';
+------------+-------------+
| table_name | column_name |
+------------+-------------+
| companies | manager_id |
| followers | user_id |
| friends | user_id |
| ... | ..._id |
+------------+-------------+
xx rows in set (0.02 sec)

参考文献

関連記事

この記事が参考になった方
ここここからチャージや購入してくれると嬉しいです(ブログ主へのプレゼントではなく、ご自身へのチャージ)
欲しいもの / Wish list