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中编写这个查询?

我已经尝试从 pricerange显示字段,但它显示了所有字段名称及其详细信息,它不处理值,从 pricerange 选择 *,但它需要一些 php 操作。但我只想用 MYSQL 编写它。

4

4 回答 4

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>

试试看 !!!

于 2012-07-24T20:14:11.520 回答
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

等等

于 2012-07-24T19:35:04.973 回答
0

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

select case "Below 500" when 1 then "Below 500" else "" end, 
       case "501-1000" when 1 then "501-1000" else "" end, etc...
于 2012-07-24T19:38:00.103 回答
0

你可以使用这样的东西:

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

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

于 2012-07-24T19:43:54.833 回答