How to return the values from a row where a value occurs for the first time among the rows available?

So what you want is to use MySQL’s “ORDER BY” feature:

SELECT Treatment FROM my_table WHERE Used=0 ORDER BY Allocation_number ASC LIMIT 1

So you look for all rows where Used=0, order (sort) them by Allocation_number in ascending order, and then pick the first row that matches. (“LIMIT 1”)

I’ll let you plug this into $wpdb as an exercise… 😉

Hope this helps!