Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL JOIN two tables with one-to-many associative array?
    primarykey
    data
    text
    <p>I'm relatively new SQL queries, and I'm trying to join two tables in order to get a nice data set, but have been having some issues. I've tried two:</p> <pre><code>SELECT wp_rg_lead.id, wp_rg_lead.created_by, wp_rg_lead_detail.field_number, wp_rg_lead_detail.value FROM wp_rg_lead, wp_rg_lead_detail WHERE wp_rg_lead.id = wp_rg_lead_detail.lead_id AND wp_rg_lead.form_id = '9' AND wp_rg_lead.payment_status = 'Approved' </code></pre> <p>and</p> <pre><code>SELECT wp_rg_lead.id, wp_rg_lead.created_by, wp_rg_lead_detail.field_number, wp_rg_lead_detail.value FROM wp_rg_lead INNER JOIN wp_rg_lead_detail ON wp_rg_lead.id = wp_rg_lead_detail.lead_id WHERE wp_rg_lead.form_id = '9' AND wp_rg_lead.payment_status = 'Approved' </code></pre> <p>Neither of which work and both of which illustrate my relative inability to make a nice SQL statement :)</p> <p>Basically, one of the tables <code>wp_rg_lead</code> contains a form id <code>wp_rg_lead.id</code> which relates to the second table <code>wp_rg_lead_detail</code> via <code>wp_rg_lead_detail.lead_id</code>.</p> <p>I basically want to get <code>wp_rg_lead.created_by</code> and associate that with the many <code>wp_rg_lead_detail.field_number</code> and their associated <code>wp_rg_lead_detail.value</code>. This has to be based on <code>wp_rg_lead.form_id = '9'</code> AND <code>wp_rg_lead.payment_status = 'Approved'</code></p> <p>Basically I'd like to end up with a nice array that would end up looking something like:</p> <pre><code>Array ( [0] =&gt; stdClass Object ( [id] =&gt; 301 [created_by] =&gt; 213 [field_number(1)] =&gt; [value(1)] [field_number(2)] =&gt; [value(2)] [field_number(3)] =&gt; [value(3)] ... ) ... ) </code></pre> <p>But I have no idea how to construct a SQL statement in order to associate the <code>field_number</code> and <code>value</code>. Do I need to construct two different statements and UNITE them?</p> <p>In the end, I would love to be able to do a count() on the array and have it only return the number of entries based on the 'Approved' status. My second query results in 208 or something.</p> <p>I'd appreciate any help trying to get this query working. This is beyond what I understand right now.</p> <p>Thanks a ton!</p> <p>Tre</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.
 

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