MySQL の複数の列に基づく行から列への動的変換
前の質問では、MySQL クエリを使用して動的に行を変換しました。単一の列の場合は列に。ここで、「data」と「price」の 2 つの列で同じ結果を達成することを目指します。
問題:
次のテーブルのデータを列に変換します。 「order」と「item」に基づいて、「order1」、「order2」、「order3」、「item1」、「item2」、「item3」、および「item4」という名前が付けられます。
id | order | data | item | Price |
---|---|---|---|---|
1 | 1 | P | 1 | 50 |
1 | 1 | P | 2 | 60 |
1 | 1 | P | 3 | 70 |
1 | 2 | Q | 1 | 50 |
1 | 2 | Q | 2 | 60 |
1 | 2 | Q | 3 | 70 |
2 | 1 | P | 1 | 50 |
2 | 1 | P | 2 | 60 |
2 | 1 | P | 4 | 80 |
2 | 3 | S | 1 | 50 |
2 | 3 | S | 2 | 60 |
2 | 3 | S | 4 | 80 |
望ましい結果:
id | order1 | order2 | order3 | item1 | item2 | item3 | item4 |
---|---|---|---|---|---|---|---|
1 | P | Q | 50 | 60 | 70 | ||
2 | P | S | 50 | 60 | 80 |
解決策:
一方、ハードコーディングされたクエリは可能です。 「order」と「item」の値の数がわかっている場合に使用されますが、一般的にはより動的なソリューションが必要です。 case.
データのアンピボット:
MySQL にはアンピボット機能がありませんが、UNION ALL を使用して複数のデータ ペアを行に変換できます:
select id, concat('order', `order`) col, data value from tableA union all select id, concat('item', item) col, price value from tableA;
値を変換して戻す列:
その後、CASE:
select id, max(case when col = 'order1' then value end) order1, max(case when col = 'order2' then value end) order2, max(case when col = 'order3' then value end) order3, max(case when col = 'item1' then value end) item1, max(case when col = 'item2' then value end) item2, max(case when col = 'item3' then value end) item3 from ( select id, concat('order', `order`) col, data value from tableA union all select id, concat('item', item) col, price value from tableA ) d group by id;
動的プリペアド ステートメント:
を使用して、ピボットされていないデータを列に変換し直すことができます。最後に、セットベースの連結を使用して、クエリを動的に準備されたステートメントに変換できます。 EXECUTE:
SET @sql = NULL; SELECT GROUP_CONCAT(DISTINCT CONCAT( 'max(case when col = ''', col, ''' then value end) as `', col, '`') ) INTO @sql FROM ( select concat('order', `order`) col from tableA union all select concat('item', `item`) col from tableA )d; SET @sql = CONCAT('SELECT id, ', @sql, ' from ( select id, concat(''order'', `order`) col, data value from tableA union all select id, concat(''item'', item) col, price value from tableA ) d group by id'); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
このアプローチは、MySQL の複数の基準に基づいて行を列に変換する柔軟かつ動的な方法を提供します。
以上がMySQL で複数の列に基づいて行を列に動的に変換する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

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

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

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

はい、それはssafetostoreblobdatainmysql、butonsiderheSeCactors:1)Storagespace:blobscanconsumesificantspace.2)パフォーマンス:パフォーマンス:大規模なドゥエットブロブスメイズ階下3)backupandrecized recized recized recize

PHP Webインターフェイスを介してMySQLユーザーを追加すると、MySQLI拡張機能を使用できます。手順は次のとおりです。1。MySQLデータベースに接続し、MySQLI拡張機能を使用します。 2。ユーザーを作成し、CreateUserステートメントを使用し、パスワード()関数を使用してパスワードを暗号化します。 3. SQLインジェクションを防ぎ、MySQLI_REAL_ESCAPE_STRING()関数を使用してユーザー入力を処理します。 4.新しいユーザーに権限を割り当て、助成金ステートメントを使用します。

mysql'sblobissuitable forstoringbinarydatawithinarationaldatabase、whileenosqloptionslikemongodb、redis、andcassandraofferferulesions forunstructureddata.blobissimplerbutcanslowdowdowd withwithdata

toaddauserinmysql、使用:createuser'username '@' host'identifidedby'password '; here'showtodoitsely:1)chosehostcarefilytoconを選択しますTrolaccess.2)setResourcelimitslikemax_queries_per_hour.3)usestrong、uniquasswords.4)endforcessl/tlsconnectionswith

toavoidcommonMonmistakeswithStringDatatypesinmysql、undultingStringTypenuste、choosetherightType、andManageEncodingandCollationsEttingtingive.1)Usecharforfixed-LengthStrings、Varcharforaible Length、AndText/Blobforlardata.2)setCurrectCherts


ホットAIツール

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

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

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

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

人気の記事

ホットツール

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

Safe Exam Browser
Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

DVWA
Damn Vulnerable Web App (DVWA) は、非常に脆弱な PHP/MySQL Web アプリケーションです。その主な目的は、セキュリティ専門家が法的環境でスキルとツールをテストするのに役立ち、Web 開発者が Web アプリケーションを保護するプロセスをより深く理解できるようにし、教師/生徒が教室環境で Web アプリケーションを教え/学習できるようにすることです。安全。 DVWA の目標は、シンプルでわかりやすいインターフェイスを通じて、さまざまな難易度で最も一般的な Web 脆弱性のいくつかを実践することです。このソフトウェアは、

Dreamweaver Mac版
ビジュアル Web 開発ツール

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません
