I have the following structure:
CREATE TABLE person {
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
email VARCHAR(30) NOT NULL UNIQUE
}
CREATE TABLE field {
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL
}
CREATE TABLE fieldPerson {
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
fieldId INT(11) UNSIGNED,
personId INT(11) UNSIGNED,
value VARCHAR(30) NOT NULL,
FOREIGN KEY (fieldId) REFERENCES field(id),
FOREIGN KEY (personId) REFERENCES person(id)
}
This is well summed up. Basically we have the default fields name and email, and we have the option to create custom fields, such as telephone, address, etc.
We can have n fields. I'm trying to figure out a way to do a SELECT query that returns all fields without making use of subqueries or extra work outside the database (it's ok using joins though).
Example (telephone is a custom field):
TABLE person:
id name email
1 Test 1 test1@gmail.com
----
TABLE field:
id name
1 telephone
2 address
----
TABLE fieldPerson:
id fieldId personId value
1 1 1 +1 555 555 555
2 2 1 First St.
----
RESULTING QUERY
personId name email telephone Address
1 Test 1 test1@gmail.com +1 555 555 555 First St.
Is that possible?
Thanks.
EDIT:
If not possible, you may suggest a optimal solution using subqueries, only with SQL though.