Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7004163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:09:49+00:00 2026-05-27T21:09:49+00:00

I am having a rather, for me, complicated mysql query on which I am

  • 0

I am having a rather, for me, complicated mysql query on which I am totally stuck and cannot find any answer for online.

Here’s my query:

SELECT
items.*
FROM
items
INNER JOIN
items_meta_data
WHERE
(
        (meta_key = 'lat' AND meta_value >= '55')
    OR
        (meta_key = 'lat' AND meta_value <= '65')
)
AND
(
        (meta_key = 'long' AND meta_value >= '20')
    OR
        (meta_key = 'long' AND meta_value <= '30')
)
GROUP BY
item_id

Of course I have tested the query with only 1 statement and that works fine. So, if I only pass the long or lat part, then I get results. Only when I try to stitch them together, I get different results.

Thanks for the help in advance!

The table structure is as follows:

Table items:
ID
item_name
item_description

Table meta:
meta_id
item_id
meta_key
meta_value

Solution

To whoever is interested I finally managed to tackle this problem. Thank you all for your help and insides.

SELECT
    SQL_CALC_FOUND_ROWS items.* 
FROM
    items
INNER JOIN
    items_meta ON (items.ID = items_meta.post_id)
INNER JOIN
    items_meta AS m1 ON (items.ID = m1.post_id)
WHERE
    1=1
    AND
    items.post_type = 'post'
    AND
    (items.post_status = 'publish')
    AND
    ( (items_meta.meta_key = 'lat' AND CAST(items_meta.meta_value AS SIGNED) BETWEEN '55'   AND '65')
    AND
    (m1.meta_key = 'long' AND CAST(m1.meta_value AS SIGNED) BETWEEN '20' AND '30') )
GROUP BY
    items.ID
ORDER BY
    items.date
DESC
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:09:50+00:00Added an answer on May 27, 2026 at 9:09 pm

    You need to consider that GROUP BY happens after the WHERE clause conditions have been evaluated. And the WHERE clause always considers only one row, meaning that in your query, the meta_key conditions will always prevent any records from being selected, since one column cannot have multiple values for one row.

    The meta_value checks given in your question also are redundant – If a value is allowed to be both smaller and greater than a given value, then its actual value doesn’t matter at all – the check can be omitted.

    But this hints at your problem – you actually want to check two separate rows of your meta table; according to one of your comments you want to check for places less than a certain distance from a given location. To get correct distances, you’d actually have to use some kind of proper distance function (see e.g. this question for details). But this SQL should give you an idea how to start, it shows how to join the item element with the two entries in the meta table that are needed (one with meta_key = ‘lat’ and one with meta_key = ‘long’):

    SELECT items.* FROM items i, meta m1, meta m2
        WHERE i.item_id = m1.item_id and i.item_id = m2.item_id
        AND m1.meta_key = 'lat' AND m1.meta_value >= 55 AND m1.meta_value <= 65
        AND m2.meta_key = 'lng' AND m2.meta_value >= 20 AND m2.meta_value <= 30
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a rather strange problem with MySQL. Trying to create a procedure to
in developing a more complicated script I am having a rather odd problem with
Ive got a rather complicated query Im running, and before I change a table
I was hoping to calculate fields using some rather complicated functions, which I don't
Im having rather an annoying problem. I have a class called Person and a
I've been having this rather frustrating problem, and having failed so many times, I'm
Having recently discovered this method of development, I'm finding it a rather nice methodology.
I am having a heck of time with something that seems rather straightforward but
I'm having a rather strange problem with zsh. When I start up my shell,
I have one main view with an associated controller. I have some rather complicated

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.