Note that there are some explanatory texts on larger screens.

plurals
  1. POmysql querying 2 columns that refer to 1 column in different table
    primarykey
    data
    text
    <p>Sorry if question has been asked but I was not able to relate any answers I found to my situation.</p> <p>I have created a temp table for this problem:</p> <pre><code>describe temp; +-----------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+--------------+------+-----+---------+-------+ | id | int(11) | NO | | 0 | | | artist | varchar(255) | NO | | NULL | | | title | varchar(255) | NO | | NULL | | | id_genre | int(11) | NO | | NULL | | | id_genre2 | int(11) | NO | | NULL | | +-----------+--------------+------+-----+---------+-------+ </code></pre> <p>The other relevent table is a list of genres.</p> <p>I want to create a query that results in a combination of these 2 queries.</p> <pre><code>select temp.title,genre.name from temp join genre on genre.id = temp.id_genre; +------------------------+--------------+ | title | genre1 | +------------------------+--------------+ | Tight Capris - 1958-B | Rockabilly | | Endless Sleep - 1958-5 | MyFavourites | | Daisy Mae - 1966 | Rockabilly | | Fire of love - 1966-58 | Rockabilly | | Stormy - 1963 | Pop | +------------------------+--------------+ 5 rows in set (0.00 sec) select temp.title,genre.name as genre2 from temp join genre on genre.id = temp.id_genre2; +------------------------+------------+ | title | genre2 | +------------------------+------------+ | Tight Capris - 1958-B | Rockabilly | | Endless Sleep - 1958-5 | Rockabilly | | Daisy Mae - 1966 | Rockabilly | | Fire of love - 1966-58 | Rockabilly | | Stormy - 1963 | Pop | +------------------------+------------+ 5 rows in set (0.00 sec) </code></pre> <p>Notice the genre difference in the 1 title: Endless Sleep</p> <p>I would like to create a result that looks like this except to replace the genre.id with the name of the genre:</p> <pre><code>+------------------------+----------+-----------+ | title | id_genre | id_genre2 | +------------------------+----------+-----------+ | Tight Capris - 1958-B | 163 | 163 | | Endless Sleep - 1958-5 | 161 | 163 | | Daisy Mae - 1966 | 163 | 163 | | Fire of love - 1966-58 | 163 | 163 | | Stormy - 1963 | 99 | 99 | +------------------------+----------+-----------+ 5 rows in set (0.00 sec) </code></pre> <p>Thanks</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.
 

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