Note that there are some explanatory texts on larger screens.

plurals
  1. POJoin 3 tables using foreign keys, with two separate fields returned from the same table
    primarykey
    data
    text
    <p>I have three tables that look like this:</p> <pre><code>People: +------------+-------------+------+-----+-------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+-------------+------+-----+-------------------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | fname | varchar(32) | NO | | NULL | | | lname | varchar(32) | NO | | NULL | | | dob | date | NO | | 0000-00-00 | | | license_no | varchar(24) | NO | | NULL | | | date_added | timestamp | NO | | CURRENT_TIMESTAMP | | | status | varchar(8) | NO | | Allow | | +------------+-------------+------+-----+-------------------+----------------+ Units: +----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | number | varchar(3) | NO | | NULL | | | resident | int(11) | NO | MUL | NULL | | | type | varchar(16) | NO | | NULL | | +----------+-------------+------+-----+---------+----------------+ Visits: +----------+-----------+------+-----+---------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+-----------+------+-----+---------------------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | vis_id | int(11) | NO | MUL | NULL | | | unit | int(11) | NO | MUL | NULL | | | time_in | timestamp | NO | | CURRENT_TIMESTAMP | | | time_out | timestamp | NO | | 0000-00-00 00:00:00 | | +----------+-----------+------+-----+---------------------+----------------+ </code></pre> <p>There are multiple foreign keys linking these tables:</p> <pre><code>units.resident -&gt; people.id visits.unit -&gt; units.id visits.vis_id -&gt; people.id </code></pre> <p>Basically, I've tried to simplify my design and keep "residents" and "visitors" together in a table, but now I'm having trouble "linking" them together with a query... Here's what I'd like to be able to return from a query:</p> <pre><code>concat(p.lname, ', ', p.fname) u.number concat(p.lname, ', ', p.fname) time_in time_out </code></pre> <p>The first set of names should be from the resident, who we know is a resident because they're referenced by the foreign key in <code>units</code>. The <code>u.number</code> would return that resident's unit number, and the second set of names would be their current visitor (who <i>hasn't</i> checked out yet, determined by the <code>0000-00-00</code> timestamp... if they checked out, it wouldn't be <code>0000-00-00</code> and thus would not be displayed).</p> <p>I'm starting to think that this either is too difficult for a novice like myself to figure out on my own, or that my DB design is flawed... The reason I wanted all residents and visitors in the same table is that I'd like them <i>all</i> to be searchable using a <code>fulltext</code> index.</p> <p>If you have suggestions, I'm all ears as I've been beating my head against a wall for days over this one...</p>
    singulars
    1. This table or related slice is empty.
    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