Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query the database where used for loops

Tags:

loops

php

mysql

I have a problem calling the database where I used for loops.

database query 1

SELECT id,qtyslot FROM rack WHERE theid = '1'

Eg id = 2 and qtyslot = 12

We create for loops

for ($x = 1; $x <= 12; $x++) {

Here I call the database with the identity of the slot varies 1 or 2,4 (with comma from 2 to 4 slots) and etc.

HOW CAN SELECT

SELECT * FROM books WHERE idrack = '2' AND slot"......." // $x

The database structure as follow.

-----------------------
| id | slot    | name |
-----------------------
| 1  |  1      | name |
-----------------------
| 2  |  2,4    | name | --> How to define this with the above loop ($x)
-----------------------
| 3  |  5,6    | name | --> How to define this with the above loop ($x)
-----------------------
| 4  |  7      | name |
-----------------------

And the result like this in table

-----------------------------------------
| loop qtyslot | In slot        | remark |
-----------------------------------------
| 1            | name slot 1    | remark |
-----------------------------------------
| 2            |                |        |
---------------|                |        |
| 3            | Name slot 2,4  | remark |
---------------|                |        |
| 4            |                |        |
-----------------------------------------
| 5            |   Name in      |        |
---------------|    slot 5,6    | remark|
| 6            |                |        |
------------------------------------------
etc up to 12

Can anyone help me.

like image 397
Haidarpesebe Avatar asked Mar 02 '26 15:03

Haidarpesebe


1 Answers

You can use SUBSTRING_INDEX function in mysql. Try This :

SELECT * FROM books WHERE idrack = '2' AND $x BETWEEN SUBSTRING_INDEX(slot, ',', 1) and SUBSTRING_INDEX(slot, ',', -1)
like image 124
Ahmed Ginani Avatar answered Mar 05 '26 03:03

Ahmed Ginani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!