I need for my game inventory function a mysql query which checks the next available free slot id.

I have item on slot 1, 2, 3 and 5.
Slot 4,6,7,8,9 ... are free..

Now I need query which would get the number 4.
Something like
SELECT * FROM inventory WHERE slot >= 1 AND slot <= 10;

But this doesnt really get the first available slot number.

Any expert knows how to do it?

4 Years
Discussion Span
Last Post by NathanOliver

id int
owner_id int
item_id int
pos int
place int

First time I hear about

SQL command "describe inventory"


How does your database store slots? If the first available slot simply isn't present in the table, a simpler approach would be to query for used slots:

select slot from inventory order by slot;

Then figure out the first unused one in code:

int next_slot = 1;

for (auto it : slots) 
    if (*it != next_slot)


Do you actuall have a slot column in the database? if so you can use

select min(slot)
from inventory
where item_id is null

I'm not quite sure if that is correct for MySQl since I use Oracle PL/SQL but that should be the gist of it.


The place row is going from 0-4. Depends how many inventory bags the user has. If he only got 1 bag then he only have place 0. If he got 2 bags then place 0 and 1.
Pos depends on the size of the bag. 4/8/12/16 are available.

select min(slot)

Does this query select the lowest number inside database?


what are the actual columns in the database? What are the types and what do they store?

The query just gives the lowest slot number availible in the inventory table where there is no item in that slot. This is if slot is a column.


I would have to agree with deceptikon on this. Read in the database into your code and then figure out where the free slot is.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.