Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your queries, reformatted:</p> <pre><code>$X: SELECT p.id FROM people_emails p WHERE p.internal_user_id = $_conf[user]-&gt;id; $Y: SELECT r.message_id , r.sender_email_id , r.receiver_email_id FROM email_routing r WHERE r.receiver_email_id = $X[id] AND r.type = $_REQUEST[type]; $R: SELECT e.subject , e.body , e.date FROM email e WHERE e.id = $Y[message_id]; $F: SELECT s.email FROM people_emails s WHERE s.id = $Y[sender_email_id] </code></pre> <p>To return these columns:</p> <pre><code>$F + $R[subject] + $R[body] + $R[date] </code></pre> <p>The queries can be combined like this:</p> <pre><code> SELECT s.email AS sender_email , e.subject , e.body , e.date FROM people_emails p JOIN email_routing r ON r.receiver_email_id = p.id AND r.type = $_REQUEST[type] JOIN email e ON e.id = r.message_id JOIN people_emails s ON s.id = r.sender_email_id WHERE p.internal_user_id = $_conf[user]-&gt;id; </code></pre> <hr> <h2>FOLLOWUP</h2> <p>To add the sender <code>full_name</code> from the <code>people</code> table, we can add a join to that table, and add the column to the SELECT list:</p> <pre><code>$N = SELECT n.full_name FROM people n WHERE n.id = (SELECT s.people_id FROM people_emails s WHERE s.email = :sender_email ) </code></pre> <p>We already have <code>:sender_email</code> from the query above, it's the value returned from <code>s.email</code> in the SELECT list, so we can just add a join to the <code>people</code> table: </p> <pre><code> SELECT s.email AS sender_email , n.full_name AS sender_full_name , e.subject , e.body , e.date FROM people_emails p JOIN email_routing r ON r.receiver_email_id = p.id AND r.type = $_REQUEST[type] JOIN email e ON e.id = r.message_id JOIN people_emails s ON s.id = r.sender_email_id JOIN people n ON n.id = s.people_id WHERE p.internal_user_id = $_conf[user]-&gt;id; </code></pre> <p>(I can't really test this, since I don't have your table definitions or sample data.)</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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