Last active
August 29, 2015 14:00
-
-
Save dha-lo-jd/11278586 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SET SESSION group_concat_max_len = 1000000; | |
SET @columns_query_1 = 'SELECT GROUP_CONCAT(CONCAT(\''; | |
SET @columns_query_2 = '.\',COLUMN_NAME) SEPARATOR \', \') into @out FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ? AND COLUMN_NAME not in ('; | |
SET @columns_query_3 = ')'; | |
SET @create_view_query_1 = 'CREATE ALGORITHM=MERGE DEFINER='; | |
SET @create_view_query_2 = ' SQL SECURITY DEFINER VIEW '; | |
SET @schema = 'スキーマ名'; | |
SET @definer = 'ユーザー名@ホスト'; | |
/* | |
* ビュー作成処理ここから ******************************************************************** | |
*/ | |
SET @view_name = 'ビューの名前'; | |
SET @table_name = 'テーブル名'; | |
SET @exclude_columns = CONCAT_WS('\',\'','カラム','カラム');-- 除外したいカラム | |
SET @exclude_columns = CONCAT('\'',@exclude_columns,'\''); | |
SET @sql = CONCAT(@columns_query_1,@table_name,@columns_query_2,@exclude_columns,@columns_query_3); | |
PREPARE stmt FROM @sql; | |
EXECUTE stmt using @table_name, @schema; | |
SET @table_columns = CONCAT(' ',@out); | |
-- カラム置換 | |
SET @replace = CONCAT(' ',@table_name,'.','カラム');-- 置換したいカラム名 | |
SET @replaced = CONCAT(@replace,' as ','Hoge_カラム');-- 置換後の文字列 | |
SET @table_columns = REPLACE(@table_columns,@replace,@replaced); | |
SET @columns = @table_columns; | |
SET @sql_from = CONCAT(' from ',@table_name); | |
/* | |
* 結合するテーブルの記述ここから | |
*/ | |
SET @table_name = 'テーブル名'; | |
SET @exclude_columns = CONCAT_WS('\',\'','カラム','カラム');-- 除外したいカラム | |
SET @exclude_columns = CONCAT('\'',@exclude_columns,'\''); | |
SET @sql = CONCAT(@columns_query_1,@table_name,@columns_query_2,@exclude_columns,@columns_query_3); | |
PREPARE stmt FROM @sql; | |
EXECUTE stmt using @table_name, @schema; | |
SET @table_columns = CONCAT(' ',@out); | |
-- カラム置換 | |
SET @replace = CONCAT(' ',@table_name,'.','カラム');-- 置換したいカラム名 | |
SET @replaced = CONCAT(@replace,' as ','Hoge_カラム');-- 置換後の文字列 | |
SET @table_columns = REPLACE(@table_columns,@replace,@replaced); | |
-- カラム置換 | |
SET @replace = CONCAT(' ',@table_name,'.','カラム');-- 置換したいカラム名 | |
SET @replaced = CONCAT(@replace,' as ','Hoge_カラム');-- 置換後の文字列 | |
SET @table_columns = REPLACE(@table_columns,@replace,@replaced); | |
-- カラムを大元に結合 | |
SET @columns = CONCAT_WS(',',@columns,@table_columns); | |
-- join句 | |
SET @sql_from = CONCAT(@sql_from,' join ',@table_name);-- joinの種類 | |
SET @join_on = ' on '; | |
SET @join_on = CONCAT(@join_on,'結合元テーブル','.','結合元カラム',' = ',@table_name,'.','結合カラム'); | |
SET @sql_from = CONCAT(@sql_from,@join_on); | |
/* | |
* 結合するテーブルの記述ここまで | |
*/ | |
SET @sql = CONCAT('select ',@columns,@sql_from); | |
SET @view_sql = CONCAT(@create_view_query_1,@definer,@create_view_query_2,@view_name,' AS ',@sql); | |
PREPARE stmt FROM @view_sql; | |
EXECUTE stmt; | |
/* | |
* ビュー作成処理ここまで ******************************************************************** | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment