sql >> Databáze >  >> RDS >> Mysql

mySQL - Vytvořte novou tabulku pomocí dat a sloupců ze tří tabulek

Musíte provést 3-way JOIN:

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'

DEMO

Nebo pokud jste již tabulku vytvořili, můžete:

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'

Chcete-li získat více atributů, musíte se spojit s tabulkami podrobností a taxonomií zvlášť pro každý atribut:

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
JOIN taxonomy AS t1 ON t1.id = d1.detail_id
JOIN details AS d2 ON d2.person_id = p.id
JOIN taxonomy AS t2 ON t2.id = d2.detail_id
JOIN details AS d3 ON d3.person_id = p.id
JOIN taxonomy AS t3 ON t3.id = d3.detail_id
WHERE t1.taxonomy = 'age' AND t2.taxonomy = 'gender' AND t3.taxonomy = 'height'



  1. Křížový dotaz PostgreSQL

  2. Jak funguje funkce LOAD_FILE() v MySQL

  3. Jaký je nejrychlejší způsob, jak zkrátit časová razítka na 5 minut v Postgres?

  4. Soubory DSN a software IRI