検索
ホームページデータベースmysql チュートリアルMySQL でセミコロンで区切られた値に基づいてテーブルを結合できますか?

Can I Join Tables in MySQL Based on Values Separated by Semicolons?

Pure MySQL でこれを解決できますか? (列内の「;」で区切られた値の結合)

質問:

「;」に基づいて結合する必要があるデータが複数のテーブルにあります。列内の値は (セミコロン) で区切られます。 PHP やその他の言語を使用して結果を操作することはできません。 MySQL のみを使用してこれを実現する方法はありますか?

答え:

はい、';' で参加することができます。純粋な MySQL を使用してカラム内の値を分離します。以下は解決策の詳細な説明です:

区切り文字列を行に変換する

重要なのは ';' を変換することです。区切り文字で区切られた文字列を一連の行に分割します。これは、次の手順を使用して実現できます。

  1. 区切り文字列内の項目の 1 から最大数までの数値を含む integerseries という名前のテーブルを作成します。
  2. COUNT_IN_SET と VALUE_IN_SET を使用します。区切り文字列内の項目の数をカウントし、その位置に基づいて各項目を抽出する関数。
  3. クロス結合を使用して、user_resource テーブルを integerseries テーブルと結合します。これにより、区切り文字列内の各項目の行が生成されます。

User_resource テーブルを正規化するクエリ

<code class="sql">SELECT user_resource.user, 
       user_resource.resources,
       COUNT_IN_SET(user_resource.resources, ';') AS resources_count, 
       isequence.id AS resources_index,
       VALUE_IN_SET(user_resource.resources, ';', isequence.id) AS resources_value
FROM 
     user_resource 
     JOIN  integerseries AS isequence 
       ON  isequence.id <p><strong>正規化されたテーブルを結合するリソース テーブル</strong></p>
<p>user_resource テーブルが正規化されたら、リソース テーブルと結合して目的の結果を得ることができます。</p>
<pre class="brush:php;toolbar:false"><code class="sql">SELECT user_resource.user, 
       resource.data

FROM user_resource 
     JOIN integerseries AS isequence 
       ON isequence.id <p><strong>サンプルの入力と出力</strong></p>
<p><strong>user_resource テーブル:</strong></p>
<table>
<thead><tr>
<th>user</th>
<th>resources</th>
</tr></thead>
<tbody>
<tr>
<td>user1</td>
<td>1;2;4</td>
</tr>
<tr>
<td>user2</td>
<td>2</td>
</tr>
<tr>
<td>user3</td>
<td>3;4</td>
</tr>
</tbody>
</table>
<p><strong>resource テーブル:</strong></p>
<table>
<thead><tr>
<th>id</th>
<th>data</th>
</tr></thead>
<tbody>
<tr>
<td>1</td>
<td>data1</td>
</tr>
<tr>
<td>2</td>
<td>data2</td>
</tr>
<tr>
<td>3</td>
<td>data3</td>
</tr>
<tr>
<td>4</td>
<td>data4</td>
</tr>
<tr>
<td>5</td>
<td>data5</td>
</tr>
</tbody>
</table>
<p><strong>Normalizeduser_resource テーブル: </strong></p>
<table>
<thead><tr>
<th>user</th>
<th>resources</th>
<th>resources_count</th>
<th>resources_index</th>
<th>resources_value</th>
</tr></thead>
<tbody>
<tr>
<td>user1</td>
<td>1;2;4</td>
<td>3</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>user1</td>
<td>1;2;4</td>
<td>3</td>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td>user1</td>
<td>1;2;4</td>
<td>3</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>user2</td>
<td>2</td>
<td>1</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>user3</td>
<td>3;4</td>
<td>2</td>
<td>1</td>
<td>3</td>
</tr>
<tr>
<td>user3</td>
<td>3;4</td>
<td>2</td>
<td>2</td>
<td>4</td>
</tr>
</tbody>
</table>
<p><strong>出力 (結合結果):</strong></p>
<table>
<thead><tr>
<th>user</th>
<th>data</th>
</tr></thead>
<tbody>
<tr>
<td>user1</td>
<td>data1</td>
</tr>
<tr>
<td>user1</td>
<td>data2</td>
</tr>
<tr>
<td>user1</td>
<td>data4</td>
</tr>
<tr>
<td>user2</td>
<td>data2</td>
</tr>
</tbody>
</table></code>

以上がMySQL でセミコロンで区切られた値に基づいてテーブルを結合できますか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
MySQL文字列タイプ:ストレージ、パフォーマンス、ベストプラクティスMySQL文字列タイプ:ストレージ、パフォーマンス、ベストプラクティスMay 10, 2025 am 12:02 AM

mysqlstringTypesimpactStorageandperformanceAseasfollows:1)churisfixed-regents、whuscanbasterbutlessspace-efficient.2)varcharisvariaible、morespace-efficient-butpotentiallyslower.3)Textisforgergetext、storedoutext、

MySQL文字列タイプの理解:Varchar、Text、CharなどMySQL文字列タイプの理解:Varchar、Text、CharなどMay 10, 2025 am 12:02 AM

mysqlstringTypesincludevarchar、テキスト、char、列挙、およびセット。1)varcharisSatileforvariaible-lengthstringsuptoaspoecifedlimit.2)TextisidealforLargetExtStorageWithDeinLength.3)charispixed-consinterconsistentalikodes.4)

MySQLの文字列データ型は何ですか?MySQLの文字列データ型は何ですか?May 10, 2025 am 12:01 AM

mysqloffersvariousstringdatatypes:1)charfixed-lengthstrings、2)varcharforvariable-lengthtext、3)binaryandvartyforbinarydata、4)blobandtextforlargedata、and5)enumandsetforControlledinput.

新しいMySQLユーザーに権限を付与する方法新しいMySQLユーザーに権限を付与する方法May 09, 2025 am 12:16 AM

tograntpermissionstonewmysqlusers、フォローステープ:1)Accessmysqlasauserwithsufthiveerprivileges、2)createanewuser withthecreateusercommand、3)usethegrantcommandtospecifypermissionsionsionsionsionsionsionsionsionsionsionselect、挿入、挿入、挿入、更新、4)

MySQLにユーザーを追加する方法:ステップバイステップガイドMySQLにユーザーを追加する方法:ステップバイステップガイドMay 09, 2025 am 12:14 AM

toadduusersinmysqucrectivally andcurally、soflowthesteps:1)usethecreateuserstatementtoaddanewuser、指定するhostandastrongpassword.2)補助金を使用して、補助金を使用して、補助すること、

MySQL:複雑な権限を持つ新しいユーザーの追加MySQL:複雑な権限を持つ新しいユーザーの追加May 09, 2025 am 12:09 AM

toaddanewuserwithpermissionsinmysql、followthesesteps:1)createtheuserwithcreateuser'newuser '@' localhost'identifiedifiedifiedifiedby'pa ssword ';。2)grantreadacestoalltablesin'mydatabase'withgrantselectonmydatabase.to'newuser'@'localhost';。3)grantwriteaccessto '

MySQL:文字列データ型とコレクションMySQL:文字列データ型とコレクションMay 09, 2025 am 12:08 AM

MySQLの文字列データ型には、CHAR、VARCHAR、バイナリ、Varbinary、BLOB、およびテキストが含まれます。照合は、文字列の比較とソートを決定します。 1.Charは固定長の文字列に適しており、Varcharは可変長文字列に適しています。 2.バイナリとVarbinaryはバイナリデータに使用され、BLOBとテキストは大規模なオブジェクトデータに使用されます。 3. UTF8MB4_UNICODE_CIなどのルールのソートは、高度と小文字を無視し、ユーザー名に適しています。 UTF8MB4_BINは症例に敏感であり、正確な比較が必要なフィールドに適しています。

MySQL:Varcharsにはどの長さを使用すればよいですか?MySQL:Varcharsにはどの長さを使用すればよいですか?May 09, 2025 am 12:06 AM

最適なMySQLVarcharの列の長さの選択は、データ分析に基づいており、将来の成長を検討し、パフォーマンスの影響を評価し、文字セットの要件を評価する必要があります。 1)データを分析して、典型的な長さを決定します。 2)将来の拡張スペースを予約します。 3)パフォーマンスに対する大きな長さの影響に注意してください。 4)ストレージに対する文字セットの影響を考慮します。これらの手順を通じて、データベースの効率とスケーラビリティを最適化できます。

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

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

ホットツール

SublimeText3 英語版

SublimeText3 英語版

推奨: Win バージョン、コードプロンプトをサポート!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

WebStorm Mac版

WebStorm Mac版

便利なJavaScript開発ツール

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

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

SublimeText3 Linux 新バージョン

SublimeText3 Linux 新バージョン

SublimeText3 Linux 最新バージョン