Title: SQL Query Issues
Last modified: August 20, 2016

---

# SQL Query Issues

 *  [Lee Wallis](https://wordpress.org/support/users/fatwombat/)
 * (@fatwombat)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/sql-query-issues/)
 * Could anyone let me know why this would not be working? I get confused between
   OR and AND 😉
 * I need to get anything that is Blue eyes or Blonde hair. At the moment this gets
   nothing.
 *     ```
       SELECT wp_posts.ID FROM wp_posts
       INNER JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
       WHERE
       	((wp_postmeta.meta_key = 'eyes' AND wp_postmeta.meta_value = 'Blue'
       	OR wp_postmeta.meta_key = 'hair' AND wp_postmeta.meta_value = 'Blonde'))
       	AND ((wp_postmeta.meta_key = 'availability' AND wp_postmeta.meta_value >= '1'
       	AND wp_posts.post_type = 'post' AND wp_posts.post_status = 'publish'))
       ```
   
 * Thanks in advance!

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [popper](https://wordpress.org/support/users/julialasarte/)
 * (@julialasarte)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/sql-query-issues/#post-2260196)
 * The problem is each of those values you are checking for is a different entry
   on the wp_postmeta table, so you’ll never find a row with both ‘eyes’ and ‘availability’
   set at the same time. Something like this might work:
 *     ```
       SELECT wp_posts.ID
       FROM   wp_posts
              INNER JOIN wp_postmeta
                ON wp_posts.ID = wp_postmeta.post_id
       WHERE  ( ( wp_postmeta.meta_key = 'eyes'
                  AND wp_postmeta.meta_value = 'Blue' )
                 OR EXISTS (SELECT *
                            FROM   wp_postmeta
                            WHERE  wp_postmeta.meta_key = 'hair'
                                   AND wp_postmeta.meta_value = 'Blonde'
                                   AND wp_postmeta.post_id = wp_posts.ID) )
              AND ( EXISTS(SELECT *
                           FROM   wp_postmeta
                           WHERE  wp_postmeta.meta_key = 'availability'
                                  AND wp_postmeta.meta_value >= '1'
                                  AND wp_postmeta.post_id = wp_posts.ID) )
              AND wp_posts.post_type = 'post'
              AND wp_posts.post_status = 'publish'
       ```
   
 *  Thread Starter [Lee Wallis](https://wordpress.org/support/users/fatwombat/)
 * (@fatwombat)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/sql-query-issues/#post-2260283)
 * WOW! That’s a bit crazy.
 * Reading through it, I understand it though, I can read it fine but if I had to
   do that myself, no hope! Haha
 * Thanks heaps for that Julia! Worked a treat 🙂

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘SQL Query Issues’ is closed to new replies.

## Tags

 * [sql](https://wordpress.org/support/topic-tag/sql/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 2 replies
 * 2 participants
 * Last reply from: [Lee Wallis](https://wordpress.org/support/users/fatwombat/)
 * Last activity: [14 years, 7 months ago](https://wordpress.org/support/topic/sql-query-issues/#post-2260283)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
