简体   繁体   中英

Getting multiple different values from a joined table

I can't figure out a good way of querying this without multiple sub-queries. I can't restructure the tables, so that's not an option. Here's the setup:

person:
person_id
person_want_id,
person_need_id,
person_ability_id,
person_willingness_id

person_status_types:
status_type_id
status_type_name

Each one of the id's that are in person is of type person_status_types. What I need to pull out is the status_type_name for each of the id's in person.

So the return statement would look like this:

person_id | person_want_name | person_need_name | person_ability_name | person_willingness_name

Right now I'm just doing 4 subqueries, but there has to be a cleaner way of doing it. Also, if you want to mention a better database structure, I'm open minded to it for future database production.

SELECT p.person_id
      ,want.status_type_name AS person_want_name
      ,need.status_type_name AS person_need_name
      ,abt.status_type_name  AS person_ability_name
      ,wil.status_type_name  AS person_willingness_name
FROM person p
LEFT JOIN person_status_types want ON p.person_want_id = want.status_type_id
LEFT JOIN person_status_types need ON p.person_need_id = need.status_type_id
LEFT JOIN person_status_types abt  ON p.person_ability_id = abt.status_type_id
LEFT JOIN person_status_types wil ON p.person_willingness_id = wil.status_type_id

Use LEFT JOIN to avoid loosing rows.
And you can't single-quote identifiers (like another answer suggests). Single quotes are for values.

Delimiter for identifier

Since this has sparked some debate, to clarify once and for all:

  • double quotes " delimit identifiers according to any ANSI standard. This is universally supported by all RDBMS that matter, MySQL being the only exception. You can fix this in MySQL with SET sql_mode = 'ANSI';

    Some RDBMS accept additional alternative delimiters like [] for SQL server.

  • single quotes ' delimit values according to the ANSI standard. Some RDBMS (like MySQL or SQL Server) tolerate them them around column names as general string delimiters. Neither accepts them around table names though.

  • backticks ´ as delimiter for identifiers are a MySQL oddity only. Again, you can fix this in MySQL with SET sql_mode = 'ANSI';

Here is a list of databases and their respective delimiters for identifiers .

You don't need to do subqueries, you can just do JOINs:

SELECT p.person_id, 
    want.status_type_name AS "person_want_name", 
    need.status_type_name AS "person_need_name", 
    ability.status_type_name AS "person_ability_name", 
    willingness.status_type_name AS "person_willingness_name"
FROM person p
JOIN person_status_types want ON p.person_want_id = want.status_type_id
JOIN person_status_types need ON p.person_need_id = need.status_type_id
JOIN person_status_types ability ON p.person_ability_id = ability.status_type_id
JOIN person_status_types willingness ON p.person_willingness_id = willingness.status_type_id

Note that the table aliases help you distinguish which "copy" of the table you're referencing, and that each one is joined on a different column from person .

Edit : The comments have identified that PostgreSQL does not allow single-quote for column aliases, which matches ANSI. Consider me surprised, since MySQL and SQL Server support it. My query has now been fixed.

Try something like this:

SELECT person_id, st1.status_type_name AS want, st2.status_type_name AS need, st3.status_type_name AS ability, st4.status_type_name AS willingness
FROM person p
LEFT OUTER JOIN person_status_types st1 ON p.person_want_id=st1.status_type_id
LEFT OUTER JOIN person_status_types st2 ON p.person_need_id=st2.status_type_id
LEFT OUTER JOIN person_status_types st3 ON p.person_ability_id=st3.status_type_id
LEFT OUTER JOIN person_status_types st4 ON p.person_willingness_id=st4.status_type_id;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM