My Table is like
CREATE TABLE IF NOT EXISTS `pricerange` (
`priceRangeID` int(11) NOT NULL AUTO_INCREMENT,
`catID` int(11) NOT NULL,
`Below 500` tinyint(1) NOT NULL DEFAULT '0',
`501-1000` tinyint(1) NOT NULL DEFAULT '0',
`1001-2000` tinyint(1) NOT NULL DEFAULT '0',
`2001-3000` tinyint(1) NOT NULL DEFAULT '0',
`3001-4000` tinyint(1) NOT NULL DEFAULT '0',
`4001-5000` tinyint(1) NOT NULL DEFAULT '0',
`5001-6000` tinyint(1) NOT NULL DEFAULT '0',
`6001-7000` tinyint(1) NOT NULL DEFAULT '0',
`7001-8000` tinyint(1) NOT NULL DEFAULT '0',
`8001-9000` tinyint(1) NOT NULL DEFAULT '0',
`9001-10000` tinyint(1) NOT NULL DEFAULT '0',
`10001-100000` tinyint(1) NOT NULL DEFAULT '0',
`above 100000` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`priceRangeID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
INSERT INTO `pricerange` (`priceRangeID`, `catID`, `Below 500`, `501-1000`, `1001-2000`, `2001-3000`, `3001-4000`, `4001-5000`, `5001-6000`, `6001-7000`, `7001-8000`, `8001-9000`, `9001-10000`, `10001-100000`, `above 100000`) VALUES
(1, 3, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1);
Here i want to extract the field name having values 1 for a given catID. Can any one help me how to write this query in mysql?
I have tried SHOW FIELDS from pricerange but it shows all field names with its details its not dealing with values, SELECT * from pricerange but it needs some php manipulations. But i want to write it in MYSQL only.
The key to solving this problem is to use the information_schema
Here is the query for you:
I loaded your data
Here is the query executed:
It also works in reverse. Here is the same query but hunting down only zeros:
Give it a Try !!!