0

I am trying to create a new table that will consist of selected data from three existing tables.

The three existing tables are set up as following, and my desired result is at the bottom:

people
id      last_name   first_name  email
1       Smith       Fred        Fred@..
2       Jones       Tom         Tom@..
3       Doe         Jane        Jane@..

taxonomy
id      taxonomy    
1       age
2       gender
3       height

details
id      person_id   detail_id   content
1       1           1           36
2       1           2           M
3       1           3           5'10"
4       2           1           29
5       2           2           M
6       2           3           6'3"
7       3           1           27
8       3           2           F
9       3           3           5'8"


New Table
id      last_name   first_name  email   age
1       Smith       Fred        Fred@.. 36
2       Jones       Tom         Tom@..  29
3       Doe         Jane        Jane@.. 27

Thanks in advance for your help!

Joelamite
  • 13
  • 2
  • What problem are you having with it? Please post the code that you're having trouble getting to work, and we'll help you fix it. – Barmar Oct 31 '14 at 00:05
  • The problem is I don't know what I'm doing. I have inherited this database as part of another project I'm working on for a non-profit, and I don't know much about mySQL except for how it works in WordPress. – Joelamite Oct 31 '14 at 03:43

1 Answers1

1

You need to do a 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

Or if you've already created the table, you can do:

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'

To get multiple attributes, you have to join with the details and taxonomy tables separately for each attribute:

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'
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I want to create a fourth table, sorry if that wasn't clear. So I would use INTO new_table after the SELECT? – Joelamite Oct 31 '14 at 04:14
  • I've updated the answer to show how to put the results into a table. – Barmar Oct 31 '14 at 15:29
  • Barmar, I must be doing something wrong - when I try to run and create a new table, a bunch of duplicates are created. The id column in the new_table looks like this: 1 2 2 3 7 9 12 14 15 17 18 22 I tried creating a table and INSERT INTO with the id set as the primary key, but then I got an error because of a duplicate... – Joelamite Oct 31 '14 at 22:21
  • My answer copies the `id` from the `people` table into the new table, since that seemed to be what you were doing in the example in the question. If you don't want to do that, leave the `id` out of both the column list and `SELECT` clause in the `INSERT` statement. Then it will assign new IDs using `AUTO_INCREMENT` (I assume you're using this for the IDs in all the tables). – Barmar Oct 31 '14 at 23:11
  • what would I add to include the other taxonomy details, i.e. gender and height. I have tried for 4 hours to figure this out, but keep running into errors. Here is the code that I am successfully using to create the fourth table, with a new primary key: `CREATE TABLE new_table (id int PRIMARY KEY AUTO_INCREMENT) AS SELECT 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'` – Joelamite Mar 26 '15 at 23:50
  • Thank you very much Barmar. You make it look so simple - I was having a really hard time with the syntax in the `WHERE` statement. – Joelamite Mar 27 '15 at 00:30
  • Performance would improve by changing `details` to have `PRIMARY KEY(person_id, detail_id)`. – Rick James Apr 30 '17 at 18:54