Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Query a Table with Multiple Foreign Keys and Return Actual Values
    primarykey
    data
    text
    <p>New to MySQL, so please bear with me.</p> <p>I'm working on a project that collects user's degrees. Users can save 3 degrees where the type, subject matter, and school are variable. These relations are normalized for other query uses so 5 tables are involved and are shown below (all have more columns then shown, just included the relevant info). The last one, 'user_degrees' is where the keys come together.</p> <p>degrees</p> <pre><code>+----+-------------------+ | id | degree_type | +----+-------------------+ | 01 | Bachelor's Degree | | 02 | Master's Degree | | 03 | Ph.D. | | 04 | J.D. | +----+-------------------+ </code></pre> <p>acad_category</p> <pre><code>+------+-----------------------------------------+ | id | acad_cat_name | +------+-----------------------------------------+ | 0015 | Accounting | | 0026 | Business Law | | 0027 | Finance | | 0028 | Hotel &amp; Restaurant Management | | 0029 | Human Resources | | 0030 | Information Systems and Technology | +------+-----------------------------------------+ </code></pre> <p>institutions</p> <pre><code>+--------+--------------------------------------------+ | id | inst_name | +--------+--------------------------------------------+ | 000001 | A T Still University of Health Sciences | | 000002 | Abilene Christian University | | 000003 | Abraham Baldwin Agricultural College | +------+----------------------------------------------+ </code></pre> <p>users</p> <pre><code>+----------+----------+ | id | username | +----------+----------+ | 00000013 | Test1 | | 00000018 | Test2 | | 00000023 | Test3 | +----------+----------+ </code></pre> <p>user_degrees</p> <pre><code>+---------+-----------+---------+---------+ | user_id | degree_id | acad_id | inst_id | +---------+-----------+---------+---------+ | 18 | 1 | 4 | 1 | | 23 | 1 | 15 | 1 | | 23 | 2 | 15 | 1 | | 23 | 3 | 15 | 1 | +---------+-----------+---------+---------+ </code></pre> <p>How can I query 'user_degrees' to find all degrees by user x, but return the actual values of the foreign keys? Taking user <code>Test3</code> as an example, I'm looking for output like so (truncated for layout's sake):</p> <pre><code>+-------------------+-------------------+-------------------+ | degree_type | acad_cat_name | inst_name | +-------------------+-------------------+-------------------+ | Bachelor's Degree | Accounting | A T Still Uni.. | | Master's Degree | Accounting | A T Still Uni.. | | Ph.D. | Accounting | A T Still Uni.. | +-------------------+-------------------+-------------------+ </code></pre> <p>I'm guessing a mix of multiple joins, temp tables and subqueries are the answer but am having trouble grasping the order of things. Any insight is much appreciated, thanks for reading.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload