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 8107009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:38:17+00:00 2026-06-06T00:38:17+00:00

I have 2 tables – posts and postmeta. posts ID title category post_status post_type

  • 0

I have 2 tables – posts and postmeta.

posts
ID   title   category  post_status  post_type    

1    ABC      cat-1    Publish      Store
2    DEF      cat-2    Publish      Store
3    GHI      cat-3    Publish      Store
4    JKL      cat-2    Publish      Store
5    XYZ      cat-5    Draft        Store
6    MNO      cat-9    Publish      Article

and

postmeta
meta_id post_id  meta_key    meta_value

109       1       city          1
110       1       featured      h
111       2       city          1,2
112       2       featured      both
113       3       city          2,3
114       3       featured      both
115       4       city          1
116       4       featured      n
117       5       city          1,4
118       5       featured      h 
119       6       city          1
120       6       featured      h

I am trying to run a query that would give me a list of posts which has the following conditions:

  1. Whose value against city has 1 AND
  2. whose value for featured is either h or both AND
  3. whose post status is Publish AND
  4. whose post type is Store
  5. Order them by title

The query I am trying is

SELECT DISTINCT posts.ID , posts.*, postmeta.*
            FROM posts, postmeta
            WHERE posts.ID = postmeta.post_id
            AND (postmeta.meta_value  = 'h' OR postmeta.meta_value = 'both') 

AND (postmeta.meta_key = 'post_city_id' AND (postmeta.meta_value LIKE '%,1,%' OR postmeta.meta_value LIKE '%1,%'  OR postmeta.meta_value LIKE '%,1%' OR postmeta.meta_value LIKE '%1%'))

            AND posts.post_status = 'Publish' 
            AND posts.post_type = 'Store'

            ORDER BY (SELECT postmeta.meta_value from postmeta where (posts.ID = postmeta.post_id) and postmeta.meta_key LIKE '%home_featured_type%') asc, posts.post_title LIMIT 0,6

The correct returns would be IDs 1 and 2 i.e. abc and def. But I am getting empty result. I cannot figure out where it is falling apart. How can this be fixed?

  • 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-06-06T00:38:20+00:00Added an answer on June 6, 2026 at 12:38 am

    here is a fixed query, but I still don’t understand the mysterious ORDER BY (SELECT) thingie.

    http://sqlfiddle.com/#!2/5ce5a/19

    SELECT DISTINCT posts.ID , posts.*, postmeta_city.*, postmeta_featured.*
    
    FROM       posts
    
    INNER JOIN postmeta AS postmeta_city
         ON     postmeta_city.post_id = posts.ID
        AND     postmeta_city.meta_key = 'city'
        AND (   postmeta_city.meta_value LIKE '%,1,%'
             OR postmeta_city.meta_value LIKE '%1,%'
             OR postmeta_city.meta_value LIKE '%,1%'
             OR postmeta_city.meta_value LIKE '%1%'
            )
    INNER JOIN postmeta AS postmeta_featured
         ON     postmeta_featured.post_id = posts.ID
        AND     postmeta_featured.meta_key = 'featured'
        AND (   postmeta_featured.meta_value = 'h'
             OR postmeta_featured.meta_value = 'both'
            )
    
    WHERE posts.post_status = 'Publish' 
      AND posts.post_type = 'Store'
    
    ORDER BY (
        SELECT postmeta.meta_value
        FROM postmeta
        WHERE ( posts.ID = postmeta.post_id )
          AND postmeta.meta_key LIKE '%home_featured_type%'
      ) asc,
      posts.title
    
    LIMIT 0,6;
    ;
    

    Updated with other people’s ideas, please upvote them:

    http://sqlfiddle.com/#!2/5ce5a/33

    SELECT DISTINCT posts.ID , posts.*, postmeta_city.*, postmeta_featured.*
    
    FROM       posts
    
    INNER JOIN postmeta AS postmeta_city
         ON postmeta_city.post_id = posts.ID
        AND postmeta_city.meta_key = 'city'
        AND FIND_IN_SET('1', postmeta_city.meta_value)
    
    INNER JOIN postmeta AS postmeta_featured
         ON postmeta_featured.post_id = posts.ID
        AND postmeta_featured.meta_key = 'featured'
        AND postmeta_featured.meta_value IN ('h','both')
    
    WHERE posts.post_status = 'Publish' 
      AND posts.post_type = 'Store'
    
    ORDER BY (
        SELECT postmeta.meta_value
        FROM postmeta
        WHERE ( posts.ID = postmeta.post_id )
          AND postmeta.meta_key LIKE '%home_featured_type%'
      ) asc,
      posts.title
    
    LIMIT 0,6;
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tables Product ( ID, CategoryID, ... ) Category ( ID, Name, ..
I have tables item and store (it's a store management system). item table has
I have tables: user, comment and post, say: A user can have many posts
I have tables (for simplicity sake) as outlined below: Category -------------------- CategoryId (0 for
For my website i have tables Category :: id | name Product :: id
I have tables A and B with identity PKs ida and idb : ex
I have tables like these two test_table date student test 2012-05-31 Alice Math 2012-05-31
I have tables like this: tblUsers int UserID string UserName tblUsersInRoles int UserID int
I have tables linked by FK, I query on the first table using entity
I have tables named news and tags_news. Given a news id I have to

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.