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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:41:57+00:00 2026-06-03T15:41:57+00:00

I have this mysql statement but I receive LIMIT in subquery error SELECT id

  • 0

I have this mysql statement but I receive LIMIT in subquery error

SELECT  id
FROM    articles
WHERE  section=1 AND id NOT IN 
        (
        SELECT  id
        FROM    articles
        WHERE   is_top_story=1  ORDER BY timestamp DESC LIMIT 2
        )

I want to select all id-s from table where section=1 and id-s not in my inner(second) statement

+--id--+section+-is_top_story-+--timestamp--+
|  54  |   1   |     1        |    130      |
|  70  |   2   |     0        |    129      |      
|  98  |   3   |     1        |    128      |
|  14  |   1   |     1        |    127      |
|  58  |   4   |     0        |    126      |
|  13  |   3   |     1        |    125      |
|  64  |   1   |     1        |    124      |
|  33  |   1   |     1        |    123      |

My sql should return 64 and 33(they are with section=1 and is_top_story=1), because 54 and 14 (are in inner statment)

If any can give me some code I will be very grateful

Thanks

  • 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-03T15:41:59+00:00Added an answer on June 3, 2026 at 3:41 pm

    How about this:

    SELECT a.id, a.times
      FROM articles AS a
      LEFT JOIN (
           SELECT id
             FROM articles
            WHERE is_top_story =1
         ORDER BY times DESC LIMIT 2) AS ax
      USING (id)
    WHERE section = 1 AND ax.id IS NULL;
    

    Join is a usual workaround when you need limits in subqueries; need for excluding logic just adds these ‘left join – joined.id IS NULL` parts to query. )

    UPDATE: Got a bit confused by your example. The original query you’ve quoted was “take some articles with section = 1, then take out those that belong to the 2 most recent top_stories”. But in your example the section should also be taken into account when selecting these stories-to-ignore-…

    It’s actually quite easy to update my query with that condition as well: just replace

    WHERE is_top_story = 1
    

    with

    WHERE is_top_story = 1 AND section = 1
    

    … but I think it might be even better solved at the client code. First, you send a simple query, without any joins whatsoever:

        SELECT id, is_top_story 
          FROM articles 
         WHERE section = 1
      ORDER BY times DESC;
    

    Then you just walk through the fetched rowset, and just ignore two first elements with ‘is_top_story’ flag on, like that:

      ...
      $topStoriesToIgnore = 2;
      foreach ($rowset as $row) {
        if ($row->is_top_story && $topStoriesToIgnore-- > 0) {
          continue;  
        }
        // actual processing code goes here
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this MySQL statement which works fine: SELECT claim_items.item_id, claim_items.quantity*items_rate.rate as per_item FROM
i have this MySQL statement from a search page, the user enters there postcode
So, I have this mysql query: SELECT member_id, text FROM review WHERE text !=
I have this simple MySQL statement: SELECT ((AVG(q1) + AVG(q8) + AVG(q15)) / 3
I'm currently working with this MySQL statement: SELECT * FROM jobs GROUP BY jobType,
I have done this successfully with an UPDATE statement before but not a REPLACE.
Looking to improve this mysql select statement for a search query: Select * from
I have this mysql statement and it's giving me error: INSERT INTO envios (
I'm having a simple select statement using php-mysql and I have this script to
I have views which look something like this: mysql> select * from p2; +---+---------+---------+

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.