2012-07-24 35 views
1

我的表像如何从值为1的表中提取字段名称?

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); 

在这里,我想提取具有给定CATID值1字段名。任何人都可以帮助我如何在MySQL中编写这个查询?

我试图从pricerangeSHOW场,但它显示了它的细节它不处理值,SELECT * FROM pricerange所有字段名,但它需要一些PHP操作。但是我只想写在MYSQL中。

+0

告诉我们您的查询... – 2012-07-24 19:28:33

+0

为什么价值可以共存在多个价格范围内? – ajreal 2012-07-24 19:35:08

+0

我的客户希望从该表中选择一个类别的范围。 – Sumant 2012-07-24 19:57:41

回答

1

解决这一问题的关键是使用INFORMATION_SCHEMA

这里是您查询:

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'; 

我装你的数据

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> 

这里是执行的查询:

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> 

它也可以反向工作。这里是相同的查询,但只搜索零:

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> 

试试看!

0

我不认为SQL是该任务的正确工具(给定您的表结构),读取整个记录并获取客户端名称会更容易。但它也可能与SQL。思想是逆透视:

SELECT 'Below 500' as field1 
FROM pricerange where catID = 1 AND `Below 500` is not null 
UNION ALL 
SELECT '501-1000' as field1 
FROM pricerange where catID = 1 AND `501-1000` is not null 

0

也许你想使用case语句...例如

select case "Below 500" when 1 then "Below 500" else "" end, 
     case "501-1000" when 1 then "501-1000" else "" end, etc... 
0

你可以使用这样的事情:

SELECT 
GROUP_CONCAT((IF `Below 500`=1, `Below 500`, ''), (IF `501-1000`=1, `501-1000`, ''),...) 
FROM `pricerange` 
WHERE `catID`=myCatID 

但如果可能的话,我会更改该数据库模式。为每个价格区间创建单独的表格和记录。

相关问题