You don’t. The way your table is structured, you’ll have to do that in your programming language (PHP?).
If your table structure was like this:
ctyID county stype svalue
1 County A s7 n
1 County A s8 n
1 County A s9 n
1 County A s10 n
2 County B s7 y
2 County B s8 n
2 County B s9 n
2 County B s10 n
Thank you Guido.
I am coding in PHP and unfortunately the table is structured as posted.
My initial thought was something like this:
SELECT *
WHERE County = “County D” AND field = ‘Y’
I thought there may be a MySQL option to reference fields within a row, but can’t seem to find one?
I suspect my other option is to return the contents of the row in an array and check each value? Are there other options I may be missing?
Thank you for your assistance.
however, if you want to return the name of a column rather than the value of a column, then you have to hardcode it –
SELECT CONCAT(
CASE WHEN s7 = 'y' THEN 's7' ELSE '' END
, CASE WHEN s8 = 'y' THEN 's8' ELSE '' END
, CASE WHEN s9 = 'y' THEN 's9' ELSE '' END
, CASE WHEN s10 = 'y' THEN 's10' ELSE '' END
) AS columns
FROM daTable
WHERE county = 'County D'
I have not ran the query yet. The real database is fairly large with 500 s-numbers. I was working on another method.
In PHP using mysqli_fetch_row($County); // to select the entire row
array_shift(); // to shift out unnecessary data
array_combine( $reference S numbers, $data (‘y’ or ‘n’) ) // Results in an array with: s7 => ‘y’, s8=>‘n’, s9=>‘y’, s10=>‘n’
Now, I just need a way to loop through the array and look for keys that match ‘y’. Haven’t reached this point yet.