Note that there are some explanatory texts on larger screens.

plurals
  1. POQuery using three terms in SQL/PHP
    primarykey
    data
    text
    <p>I'm using WordPress, but this question is more pertaining to the SQL involved. I'll gladly move it if I need to.</p> <p>I'm working on <a href="http://www.libertyguide.com/jobs" rel="nofollow">http://www.libertyguide.com/jobs</a> and I'm trying to alter the filtering mechanics. Currently it's a global <code>OR</code> query.</p> <p>Anyways, I have three filtering lists, and I'm storing what's selected into three strings (interests, type, experience) in the following way:</p> <pre><code>"( $wpdb-&gt;terms.slug = 'webdevelopment' OR $wpdb-&gt;terms.slug = 'journalism' OR ... ) AND" </code></pre> <p>It's populated by whatever is selected in my filtering lists.</p> <p>When it comes down to it, I have this as a basic query (I'm leaving out the <code>LEFT JOINS</code>):</p> <p>Before:</p> <pre><code>SELECT * FROM $wpdb-&gt;posts WHERE ($wpdb-&gt;terms.slug = 'fromlist1' OR $wpdb-&gt;terms.slug = 'fromlist2' OR $wpdb-&gt;terms.slug = 'fromlist3') AND $wpdb-&gt;term_taxonomy.taxonomy = 'jobtype'... </code></pre> <p>After:</p> <pre><code>SELECT * FROM $wpdb-&gt;posts WHERE ($wpdb-&gt;terms.slug = 'fromlist1' OR $wpdb-&gt;terms.slug = 'fromlist1again') AND ($wpdb-&gt;terms.slug = 'fromlist2' OR $wpdb-&gt;terms.slug = 'fromlist2again') AND ($wpdb-&gt;terms.slug = 'fromlist3' OR $wpdb-&gt;terms.slug = 'fromlist3again') AND $wpdb-&gt;term_taxonomy.taxonomy = 'jobtype'... </code></pre> <p>So essentially I want to go from an </p> <p><code>OR</code> filter </p> <p><strong>to</strong> </p> <p>an <code>AND</code> filter with <code>OR</code> filtering inbetween.</p> <p>My new filtering only works when one item overall is selected, but returns nothing when I select more than one thing (that I know would match up with a few posts).</p> <p>I've thought through the logic and I don't see anything wrong with it. I know nothing is wrong with anything else, so it has to be the query itself.</p> <p>Any step in the right direction would be greatly appreciated. Thanks!</p> <p><strong>UPDATE</strong> From the confusion, basically I have this:</p> <pre><code>"SELECT ...... WHERE $terms ..." </code></pre> <p>but I WANT </p> <pre><code>"SELECT ....... WHERE $interests AND $type AND $experience" </code></pre> <p>I don't want to have it filter <code>$interest[1] OR $interest[2] OR $type[1] OR $experience[1]</code>, but instead want it to filter <code>($interest[1] OR $interest[2]) AND ($type[1]) AND ($experience[1])</code></p> <p>I hope this makes more sense</p> <p>*<strong><em>UPDATE 2</em>*</strong></p> <p>Here's and example:</p> <p>In my interests list, I select for example three things: WebDevelopment, Academia, Journalism.</p> <p>In my type list, I choose two things: Fulltime, Parttime</p> <p>In my experience list, I choose three things: Earlycareer, Midcareer, Latecareer.</p> <p>When I run my query, I want to make sure that each record has AT LEAST one of each of the three lists. Possible Results: (WebDevelopment, Parttime, Midcareer), (Academia, Fulltime, Earlycareer, Midcareer).</p> <p>NOT A RESULT: (Journalism, Earlycareer) - missing fulltime or parttime</p> <p>I really hope this clears it up more. I'm willing to give compensation if I can get this working correctly.</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