mySQL:從三個表的資料和欄位建立新表
問題:
問題:問題:
問題:
CREATE TABLE new_table AS SELECT p.*, d.content AS age FROM people AS p JOIN details AS d ON d.person_id = p.id JOIN taxonomy AS t ON t.id = d.detail_id WHERE t.taxonomy = 'age';
如何建立一個新表,組合從三個現有表中選擇的數據,包括欄位last_name、來自人員、詳細資料和分類表的名字、電子郵件和年齡?
答案:
要實現此目的,您可以使用 3 路 JOIN。
INSERT INTO new_table (id, last_name, first_name, email, age) SELECT p.id, p.last_name, p.first_name, p.email, d.content AS age FROM people AS p JOIN details AS d ON d.person_id = p.id JOIN taxonomy AS t ON t.id = d.detail_id WHERE t.taxonomy = 'age';
建立一個新資料表連接:
此查詢將建立一個名為new_table 的新表,其中包含姓氏、名字、電子郵件和年齡列。詳細資料表中的 content 欄位用於儲存年齡資訊。
CREATE TABLE new_table AS SELECT p.*, d1.content AS age, d2.content AS gender, d3.content AS height FROM people AS p JOIN details AS d1 ON d1.person_id = p.id AND d1.taxonomy_id = (SELECT id FROM taxonomy WHERE taxonomy = 'age') JOIN details AS d2 ON d2.person_id = p.id AND d2.taxonomy_id = (SELECT id FROM taxonomy WHERE taxonomy = 'gender') JOIN details AS d3 ON d3.person_id = p.id AND d3.taxonomy_id = (SELECT id FROM taxonomy WHERE taxonomy = 'height');插入資料到現有表中:如果您已經建立了new_table,則可以使用下列查詢將資料插入其中:加入多個屬性:要從其他表格中擷取多個屬性(例如年齡、性別、身高),您需要執行額外的連結:
以上是如何將三個 MySQL 表中的資料合併到一個新表中?的詳細內容。更多資訊請關注PHP中文網其他相關文章!