Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL 3 tables single query for Datatables
    primarykey
    data
    text
    <p>I'm trying to get data from 3 tables in one query. Those tables are for storing and displaying user messages. Each user can log in and request all his inbox messages. The user id is avaliable from the PHP session and parameter type=1 gets passed in the php request which means the request to show inbox (type=2 would be info messages etc).</p> <p>First table called <strong>email</strong> has <code>id</code>, <code>subject</code>, <code>body</code> and <code>date</code></p> <p>Second table called <strong>email_routing</strong> has <code>message_id</code>, <code>sender_email_id</code>, <code>receiver_email_id</code>, <code>basket</code>, <code>type</code>. The <code>message_id</code> refers to <code>id</code> of the message in <strong>email</strong> table. </p> <p>Sender and receiver id refers to email ids from the third table that called <strong>people_emails</strong>. It consist of <code>id</code>(that what sender and receiver email id refer to), <code>internal_user_id</code> (that's what is avaliable from PHP session), <code>email</code> and <code>people_id</code> (each person may have several emails in the contact book so I store people and their emails in separate tables, but this last column if not of our interest here).</p> <p>So I can figure out how I would do my queries in sequence and just for one row result:</p> <p>Fist I would get user email id based on his user_id from PHP session:</p> <pre><code>$X = SELECT `id` from `people_emails` WHERE `internal_user_id`=$_conf[user]-&gt;id; </code></pre> <p>$X is not a proper coding, let's say it is a variable I would eventually get.</p> <p>Then I can query the email_routing table to all the messages addressed to that user:</p> <pre><code>$Y = SELECT `message_id`, `sender_email_id`, `receiver_email_id` FROM `email_routing` WHERE `receiver_email_id`=$X[id] and `type`=$_REQUEST[type]; </code></pre> <p>Having list of the message_id's I can now query the email table for the messages content:</p> <pre><code>$R = SELECT `subject`, `body`, `date` FROM `email` WHERE `id`=$Y[message_id]; </code></pre> <p>Ok, then I need to "translate" the "from" addresses from its id which I got from email_routing table, so it is:</p> <pre><code>$Z_from = SELECT `email` FROM `people_emails` WHERE `id`=$X[sender_email_id]; </code></pre> <p>I now have all the data for one row. Then I need to put all together which will be</p> <pre><code>$Z_from + $R[subject] + $R[body] + $R[date] </code></pre> <p>I need to have it as one list of results which then gets passed to bootstrap datatables plugin for displaying and searching. I can't seem to figure that one out. Please help.</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