1

I have an array of fields (not rows!) I want to loop through, in a table, in a predefined order, backwards (alternatively, I could reverse the order of the array) and return the field name of the first field that returns a predefined value.

For example, the fields are like this:

field_a => 1
field_b => 0
field_c => 1
field_d => 0
field_e => 0

And say, field_c was the first with the value I was looking for, even though field_a may already possess the value. What is the best way to loop through e through a, and stopping at c when it's the first field with the value, and returning that field name?

I am working with PHP and MySQL, so I would prefer the returned field name to be a string, not an array - I don't need the value of the row();, only the field name itself.

Thank you! :)

3
  • Your requirements are contradictory: at the top you're saying you need the row name and value, but at the bottom you're saying you only need the row name. Which is it? Commented Jan 29, 2010 at 23:34
  • 2
    What exactly do you mean by "row name"? The row's primary key? Also, you way want to read over this article: blogs.msdn.com/simonince/archive/2009/03/30/… - even though it's about SQL Server - it discusses how when working with databases you should start thinking of things as set based (vs procedural which is how most programming works). Commented Jan 29, 2010 at 23:41
  • By row name, I mean the field name. Let me edit my original post. Commented Jan 29, 2010 at 23:48

1 Answer 1

2

Would this work?

SELECT *
FROM yourtable
WHERE criteria-for-property
ORDER BY whatever-ordering-you-want
LIMIT 1

This would retrieve 1 row and then stop.

Sign up to request clarification or add additional context in comments.

2 Comments

Which, if you read the question carefully, is what he asks for. He says "That value" and "return the row name and value of the first row that returns a predefined value.".
Added for clarity; I want the returned field name.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.