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.
Use
LEFT JOINto 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 withSET 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 withSET sql_mode = 'ANSI';Here is a list of databases and their respective delimiters for identifiers.