从表中选择value = something的所有数据

时间:2014-12-11 18:54:29

标签: php mysql

我有一张桌子

map1

cham1 , cham2 , cham3 , cham4 , cham5 , cham6 , cham7 , cham8 , cham9 

某些列的值为100,有些列的值为200

我想选择值为100或值为200的列

SQL查询应该是这样的:

select cham1,cham2,cham3,cham4,cham5 from map1 where value of  = 100 ;

我希望从表格列

中选择值为100的列

3 个答案:

答案 0 :(得分:2)

您可能需要以下内容:

SELECT *
FROM map1
WHERE cham1 IN (100, 200)
  OR cham2 IN (100, 200)
  OR cham3 IN (100, 200)
  OR cham4 IN (100, 200)
  OR cham5 IN (100, 200)
;

此查询听起来好像存储在5列(cham1 .. cham5)值中,这些值应存储在单个列中。或者,最有可能的是,他们需要在一个单独的表中保留(在同一列中)。

答案 1 :(得分:1)

这应该有效 -

SELECT *
FROM `map1`
WHERE `cham1` IN (100, 200)
OR `cham2` IN (100, 200)
OR `cham3` IN (100, 200)
OR `cham4` IN (100, 200)
OR `cham5` IN (100, 200)

答案 2 :(得分:0)

“想要选择列”是什么意思?列名?或该列的值?

这将为逗号分隔的列名称设置为100或200

SELECT 
   CONCAT(
      CASE WHEN cham1 IN (100,200) THEN 'cham1,' ELSE '' END, 
      CASE WHEN cham2 IN (100,200) THEN 'cham2,' ELSE '' END, 
            . . . 
      CASE WHEN cham5 IN (100,200) THEN 'cham5,' ELSE '' END
) FROM MAP1

这将只选择100s和200s

SELECT 
      cham1 = CASE WHEN cham1 IN (100,200) THEN cham1 ELSE NULL END, 
      cham2 = CASE WHEN cham2 IN (100,200) THEN cham2 ELSE NULL END, 
            . . . 
      cham5 = CASE WHEN cham5 IN (100,200) THEN cham5 ELSE NULL END
FROM MAP1

如果您想限制只有100或200的行,您可以添加where子句

WHERE cham1 IN (100, 200)
OR cham2 IN (100, 200)
OR cham3 IN (100, 200)
OR cham4 IN (100, 200)
OR cham5 IN (100, 200)