sql - Selecting from mulitple tables, then joining to both, in MySQL -
Say I have a database structure like:
create table person (iderson The name of the ETT nail atangram primaries, named VARCHAR (50)); Type the table description (type INT not pure AUTO_INCREMENT primary key, field VARCHAR (50)); Create table details (IDPerson not INT, idDetailType is not INT, value VARCHAR (50)); with this data:
| ---------- | --------- | | IDPerson | Name | | ---------- | --------- | | 1 | Tom | | 2 | Dick | | 3 | Harry | | ---------- | --------- | | ---------- | ---------- | | Type | Field | | ---------- | ---------- | | 1 | Diet | 2 | Cap size | ---------- | ---------- | | ---------- | ------ | ------------ | | IDPerson | Type | Price | | ---------- | ------ | ------------ | | 1 | 1 | Vegetarian | | 1 | 2 | Medium | | 2 | 2 | Short | | ---------- | ------ | ------------ | And I want a result that looks like this:
| ---------- | ------- --- | ------------ | | Name | Field | Price | | ---------- | ---------- | ------------ | | Tom | Diet Vegetarian | | Tom | Cap size Medium | | Dick | Diet Faucet | Dick | Cap size Short | | Harry | Diet Faucet | Harry | Cap size Faucet | ---------- | ---------- | ------------ | I think if I make a mistake, then correct me, who should get it, should look like this:
SELECT name, Field, value, type P, type description, leaving the statement d on p.idPerson = d.idPerson and t.type = d.type but MySQL (5.5.32) / PHPMyAdmin complains that "unknown" column 'p.idPerson' on the 'clause' you can do it around:
select the name, field, type description by value, person p Leave Details Details But D.PPD = d.idPerson and t.type = d.type and it has only one complaint, except that t.type is now unknown.
Current solution is a [shiver] nested query, like this: SELECT name, field, value FROM (SELECT type, field, idPerson from person P, Type the details) Pte join the left pt.idPerson = p.idPerson and pt.type = t .type Is this a known problem with MySQL? Is it my first question Just plain are wrong? Can I do it better? Because it does not seem right to me ....
There is no "Type" field in your table Did you want to use idDetailType ? Try the following: SELECT * Type the tippet in detail Leave the person p Description Details on D. DPs = Type D.IDPsaran and T.IDDrDType = D.Detect Type then that dirty code * PD: Field and Value Refine results to avoid using reserved keywords. This may cause some unpredictable behavior.
Comments
Post a Comment