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

  • Home
  • SEARCH
  • 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 8394715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:07:45+00:00 2026-06-09T20:07:45+00:00

I have a table with the following info: id | Item ID | stock_package_id

  • 0

I have a table with the following info:


id |      Item ID     | stock_package_id | amount | price
0  |        775       |         1        |   16   |  0.22
1  |        758       |         2        |   10   |  0.28
2  |        775       |         3        |   10   |  0.30
3  |        774       |         4        |   10   |  0.25
4  |        775       |         5        |   10   |  0.10
5  |        775       |         6        |   10   |  0.55


My issue is i’m trying to create a query (simple as possible as this table will be left joined with a few other tables) that will allow me to enter an amount:

e.g.

I want 22 of Item_Id 775 selecting from the cheapest price first.

So what I would want returned is:


id |      Item ID     | stock_package_id | amount | price
4  |        775       |         5        |   10   |  0.10
0  |        775       |         1        |   12   |  0.22 - this is only 12 as we only want 22 total

a pesudo example:

select from stock_table until the sum of amount is equal to or greater than input number (22) order by price

Is this possible to do with MySql?

  • 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-09T20:07:47+00:00Added an answer on June 9, 2026 at 8:07 pm

    Schema:

     CREATE TABLE table1
    (`id` int, `Item_ID` int, `stock_package_id` int, `amount` int, `price` float);
    
     INSERT INTO table1
    (`id`, `Item_ID`, `stock_package_id`, `amount`, `price`)
     VALUES
    (0, 775, 1, 16, 0.22),
    (1, 758, 2, 10, 0.28),
    (2, 775, 3, 10, 0.30),
    (3, 774, 4, 10, 0.25),
    (4, 775, 5, 10, 0.10),
    (5, 775, 6, 10, 0.55);
    

    Setting amount to get:

    SET @var_amount = 22;
    

    Selecting amount:

    SELECT * FROM (
    SELECT table1.*, if ((@var_amount := @var_amount - amount) > 0, amount, amount +  @var_amount) as buy_amount
    FROM table1
    WHERE Item_ID = 775
    ORDER BY price ASC
    ) AS tmp 
    WHERE buy_amount > 0
    

    Result:

    id  Item_ID stock_package_id    amount  price   buy_amount
    4   775 5                       10      0.1     10
    0   775 1                       16      0.22    12
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well i have the following table(info from pgAdmin): CREATE TABLE comments_lemms ( comment_id integer,
I have the following setup in MS IE8: <table class=my-info> <tbody> <tr> <td class=info-left>First
I have DB2 table having following structure CREATE TABLE DUMMY ( ID CHARACTER(10) NOT
I have a table Pet.Info like the following in SQL Server : pet age
I have a table containing the following details: date_info info 2009-06-23 1 2009-06-24 2
I have table with following structure: <table class=table-info border='1px' > <tr > <td class=view
Hi This my second question here. I have the following table |-----|-------|------|------| |._id.|..INFO.|.DONE.|.LAST.| |..1..|...A...|...N..|......|
I have a table like the following : ID |INFO | DATE_DT ------------------------- 1091|info5
I have the following table in PostgreSQL 8.4.5: snake=> create table gps ( id
I have one product table with following info. ID ProductId Name OtherData 1 0

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.