Skip to content
Advertisement

Fetch Data from ID Range and Store in a separate PHP Array – Complex MySQL Query

I have to do a very unusual Mysql query.

I have 1 Table with 2 listings over currently 5k lines that looks like this:

JavaScript

And so on its all very basic, but now I have to filter it out in a specific order for example:

JavaScript

In this Case the query should give me the Ids 2370 – 2374. These Ids should be saved in a php Array or separate PHP Variables. Array is preferred though.

My Question whats the best way to do this.

For those who might want to claim this Question as a duplicate, I have no idea of how to describe this procedure so I was unable to Google it.

Edit:

Example Order This Can be Anything:

JavaScript

I want to Filter in which line the the first 2 comes like you see in my first code Block its the line with id 2370 after that the same with the 0

One line is depending on its bottom and top line

If I would change Replace the 0 in my Example with a 2 for example none of the id´s in the first code block would be true

I want to check every line from the first till the latest id if this order pops up

Advertisement

Answer

Disclaimer – A bad way to do this 😀 Consider this maybe only when you have to execute this manually or optimization is not an issue.

From your first example 2370 -> 2372 I took the assumption that your ids might skip some numbers but you would still want to match “the next row”, so firstly I created an ugly sub-table to get the id-pair of 4 consecutive ids

JavaScript

and then filtered out only the rows matching the color-ordering you wanted

JavaScript

Here’s the link to fiddle

Now if you would normalize your ids so they wount skip numbers either on saving/deleting records or rearranging over time, the solution would be simple:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement