Note that there are some explanatory texts on larger screens.

plurals
  1. POINNER or LEFT Joining Multiple Table Records Into A Single Row
    primarykey
    data
    text
    <p><strong>Phone Table</strong></p> <pre> +----------------+-------------+ | Field | Type | +----------------+-------------+ | f_id | int(15) | | f_client_id | int(11) | | f_phone_type | varchar(50) | | f_phone_number | varchar(13) | +----------------+-------------+ </pre> <p><strong>Clients Table</strong></p> <pre> +-----------------------------+--------------+------+-----+ | Field | Type | Null | Key | +-----------------------------+--------------+------+-----+ | f_id | int(15) | NO | PRI | | f_first_name | varchar(13) | YES | MUL | | f_mi | char(1) | YES | | | f_last_name | varchar(20) | NO | MUL | +-----------------------------+--------------+------+-----+ </pre> <p><strong>Assumptions:</strong></p> <ol> <li>Each record in 'Phone Table' belongs to one record in 'Clients Table'.</li> <li>Each record in 'Clients Table' can have 0 or more records in 'Phone Table'.</li> <li>Simple Translation: A client can have 0 or more phone numbers</li> </ol> <p><strong>With a standard LEFT or INNER join, I get something like this:</strong></p> <pre> +------------+------------+--------------+ | name | Phone Type | Phone Number | +------------+------------+--------------+ | John Smith | Home | 712-555-6987 | | John Smith | Work | 712-555-1236 | +------------+------------+--------------+ </pre> <p><strong>I need a query that will give me the work and home numbers that belong to a given client:</strong></p> <pre> +------------+----------------+--------------+ | Name | Work Number | Home Number | +------------+----------------+--------------+ | John Smith | 712-555-1236 | 712-555-6987 | +------------+----------------+--------------+ </pre> <p>Is it possible to do a LEFT or INNER join and then merge those results into a single row? I've seen similiar questions on this, but the examples given were much more complex than what I'm after:</p> <p><strong>Similiar Questions</strong></p> <ul> <li><a href="https://stackoverflow.com/questions/2485210/mysql-need-help-constructing-query-join-multiple-tables-into-single-row">MySQL Need help constructing query: join multiple tables into single row</a></li> <li><a href="https://stackoverflow.com/questions/2561280/sql-left-join-with-multiple-rows-into-one-row">SQL left join with multiple rows into one row</a></li> </ul> <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.
 

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