Postgres で列を複数の行に分割する
次のような複数の値を含む列を持つテーブルがあるとします。
subject | flag --------+----- this is a test | 2
このテーブルを、元の列の各値が個別になる新しい構造に変換する必要がある場合があります。次のような行:
token | flag --------+----- this | 2 is | 2 a | 2 test | 2
LATERAL Join を使用したソリューション (Postgres 14)
Postgres 14 以降では、次の LATERAL Join を使用してこれを達成する効率的な方法が提供されます。 string_to_table() 関数。この関数は、対象の列をトークンの配列に分割します。
SELECT token, flag FROM tbl, string_to_table(subject, ' ') AS token WHERE flag = 2;
string_to_array() による解決策 (Postgres 13-)
Postgres 13 以前の場合は、次を使用します。代わりに string_to_array() 関数string_to_table():
SELECT unnest(string_to_array(subject, ' ')) AS token, flag FROM tbl WHERE flag = 2;
正規表現を使用した代替解決策
もう 1 つのオプションは、string_to_table() よりも効率が劣りますが、regexp_split_to_table() 関数を使用することです。アプローチ:
SELECT s.token, t.flag FROM tbl t CROSS JOIN regexp_split_to_table(subject, ' ') AS s(token) WHERE t.flag = 2;
追加メモ
- WHERE 句は、フラグ値に基づいて結果をフィルタリングするために使用されます。
- FROM リスト内のカンマは、FROM 内のセットを返す関数に対して自動的に想定される CROSS JOIN を表します。 list.
- 件名の列が空または null の場合は、ON TRUE を指定した LEFT JOIN を使用して、元のテーブルから修飾された行を保持します。
以上がPostgreSQLで列を複数の行に分割するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

todropaviewinmysql、 "dropviewifexistsview_name;" andtomodifyaviewを使用して、 "createorreplaceviewview_nameasselect ..."を使用します

mysqlviewscanefectiveativeativeizedesignpatternslikeadapter、decorator、factory、andobserver.1)adapterpatternadaptsdatafromdifferenttablesintoaunifiedview.2)decoratorpatternenhancesdatawithedfieldsfieldsiffieldsiffieldsiffiedを

viewsinmysqlarebenefentialforsimprifiningcomplexqueries、拡張セキュリティ、ダタコンシーニング、および最適化されたパフォーマンスを保証する1)itsmplifyififycomplexqueriesbyencapsulsingthemintoreusableviews.2)viewsencurationecuritybycontrollingcescesces.3)

to CreateAsimpleviewinmysql、usethecreateviewstatement.1)DefinetheTheTheThecreateview_nameas.2)SpecifyTheSelectStatementtatementtatementtatementtatementtatementtatementedeSireddata.3)

tocleateusersinmysql、usethecreateuserstatement.1)foralocaluser:createUser'localuser '@' localhost'identifidedifiedifiedified 'securepassword';

mysqlviewshavelimitations:1)supportallsqloperations、制限、dataManipulationswithjoinsorubqueries.2)それらは、特にパフォーマンス、特にパルフェクソルラージャターセット

reperusermanmanagementInmysqliscialforenhancingsecurationsinginuring databaseaperation.1)usecreateusertoaddusers、指定connectionsourcewith@'localhost'or@'% '。

mysqldoes notimposeahardlimitontriggers、しかしpracticalfactorsdeTerminetheireffectiveuse:1)serverconufigurationStriggermanagement; 2)complentiggersincreaseSystemload;


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

WebStorm Mac版
便利なJavaScript開発ツール

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

MinGW - Minimalist GNU for Windows
このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

SublimeText3 中国語版
中国語版、とても使いやすい

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)
