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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:30:46+00:00 2026-05-27T01:30:46+00:00

Imagine a MySql table like this: ————————— ListID | itemID | Item ————————— List_1

  • 0

Imagine a MySql table like this:

---------------------------
ListID | itemID | Item
---------------------------
List_1 | item_1 | Apple
---------------------------
List_1 | item_2 | Orange
---------------------------
List_1 | item_3 | Pear
---------------------------
List_2 | item_1 | Potatoes
---------------------------
List_2 | item_2 | ...

I have a getNextItem() function which increments the itemID and use a SQL query to get the next item:

SELECT * FROM items_tbl
WHERE listID = "$listId"
AND itemID = "$itemID"
LIMIT 1

Is there an easy way in SQL to figure out if the item I am getting is the last one of that list?

I could add a column to the table and mark the last item of each list, or I could make another query, get the highest item number and compare with my list the current item, or get the total number of items in the list and keep track of the item number in relation to the list’s total… but all these solutions feel like a hack.

Can someone suggest something better in SQL and explain a bit how it works?

  • 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-27T01:30:47+00:00Added an answer on May 27, 2026 at 1:30 am

    You did not state your DBMS, but the following is an ANSI compliant SQL (should work on PosgreSQL, Oracle, DB2)

    SELECT *
    FROM (
        SELECT listid, 
               itemid,
               case 
                  when lead(itemid) over (partition by listid order by itemid) is null then 'last'
                  else 'not_last'
               end as last_flag
        FROM items_tbl
        WHERE listID = 'List_1'
    ) t
    WHERE itemID = 'item_2'
    

    Edit, the following should work on SQL Server (as that doesn’t yet support lead()):

    SELECT listid, 
           itemid,
           case 
             when rn = list_count the 'last'
             else 'not_last'
           end
    FROM (
        SELECT listid, 
               itemid,
               row_number() over (partition by listid order by itemid) as rn,
               count(*) over (partition by listid) as list_count
        FROM items_tbl
        WHERE listID = 'List_1'
    ) t
    WHERE itemID = 'item_2'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having the following problem. Imagine my mySQL table looks like this: id range
Let me explain this a bit. Imagine I have a table like this: id
I have some data in this mysql table, but it is not showing anything
basically im trying to make the ubiquitous like button, and update a MySQL table
I would like to implement a basic versioning system in a MySQL table. Let's
Imagine this directory structure: app/ __init__.py sub1/ __init__.py mod1.py sub2/ __init__.py mod2.py I'm coding
Possible Duplicate: MySQL check if a table exists without throwing an exception I have
I have a very large MySql table: over 900 rows of different sentences. I
Imagine the following tables: create table boxes( id int, name text, ...); create table
I have a table that has a view_count column. I would like to increment

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.