Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8283463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:44:40+00:00 2026-06-08T10:44:40+00:00

My Table is like CREATE TABLE IF NOT EXISTS `pricerange` ( `priceRangeID` int(11) NOT

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T10:44:41+00:00Added an answer on June 8, 2026 at 10:44 am

    The key to solving this problem is to use the information_schema

    Here is the query for you:

    SELECT A.column_name FROM
    (select ordinal_position-3 pos,column_name from information_schema.columns
    where table_name='pricerange' and LOCATE('-',column_name)) A
    INNER JOIN
    (SELECT CONCAT(`501-1000`,`1001-2000`,`2001-3000`,
    `3001-4000`,`4001-5000`,`5001-6000`,`6001-7000`,
    `7001-8000`,`8001-9000`,`9001-10000`,
    `10001-100000`) bitmap FROM pricerange) B
    ON SUBSTR(bitmap,pos,1) = '1';
    

    I loaded your data

    mysql> DROP DATABASE IF EXISTS sumant;
    Query OK, 1 row affected (0.06 sec)
    mysql> CREATE DATABASE sumant;
    Query OK, 1 row affected (0.00 sec)
    mysql> USE sumant
    Database changed
    mysql> 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 ;
    Query OK, 0 rows affected (0.17 sec)
    
    mysql> 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);
    Query OK, 1 row affected (0.07 sec)
    
    mysql>
    

    Here is the query executed:

    mysql> SELECT A.column_name FROM
        -> (select ordinal_position-3 pos,column_name from information_schema.columns
        -> where table_name='pricerange' and LOCATE('-',column_name)) A
        -> INNER JOIN
        -> (SELECT CONCAT(`501-1000`,`1001-2000`,`2001-3000`,
        -> `3001-4000`,`4001-5000`,`5001-6000`,`6001-7000`,
        -> `7001-8000`,`8001-9000`,`9001-10000`,
        -> `10001-100000`) bitmap FROM pricerange) B
        -> ON SUBSTR(bitmap,pos,1) = '1';
    +--------------+
    | column_name  |
    +--------------+
    | 5001-6000    |
    | 6001-7000    |
    | 9001-10000   |
    | 10001-100000 |
    +--------------+
    4 rows in set (0.02 sec)
    
    mysql>
    

    It also works in reverse. Here is the same query but hunting down only zeros:

    mysql> SELECT A.column_name FROM
        -> (select ordinal_position-3 pos,column_name from information_schema.columns
        -> where table_name='pricerange' and LOCATE('-',column_name)) A
        -> INNER JOIN
        -> (SELECT CONCAT(`501-1000`,`1001-2000`,`2001-3000`,
        -> `3001-4000`,`4001-5000`,`5001-6000`,`6001-7000`,
        -> `7001-8000`,`8001-9000`,`9001-10000`,
        -> `10001-100000`) bitmap FROM pricerange) B
        -> ON SUBSTR(bitmap,pos,1) = '0';
    +-------------+
    | column_name |
    +-------------+
    | 501-1000    |
    | 1001-2000   |
    | 2001-3000   |
    | 3001-4000   |
    | 4001-5000   |
    | 7001-8000   |
    | 8001-9000   |
    +-------------+
    7 rows in set (0.01 sec)
    
    mysql>
    

    Give it a Try !!!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Created a table like CREATE TABLE [dbo].[tab1]( [Id] [int] NOT NULL, [Name]
I have mysql database structure like below: CREATE TABLE test ( id int(11) NOT
So I have two tables structured like so: CREATE TABLE #nodes(node int NOT NULL);
Given: CREATE TABLE foo (id BIGINT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id)); I'd like to
I have a table like create table adjacencies( relationId int not null primary key
I have a table like create table adjacencies( relationId int not null primary key
I have two table like below CREATE TABLE IF NOT EXISTS `countries` ( `id`
I've got a table like so: CREATE TABLE IF NOT EXISTS grades(_id, timestamp, extra);
Given a table like: CREATE TABLE MyTable ( MyColumn NUMBER NOT NULL ); I
If I have a table like CREATE TABLE [FooTable]( [foo] [varchar](50) NOT NULL, [foo_boo]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.