如何从具有特定条件的表中查找列名?

时间:2013-10-02 12:52:00

标签: php mysql sql database

我想找到列名为1的列名,其中userid = 3 例如我有一张桌子

问题

 quesitonid   userid      answer1 answer2 answer3 
  1            1          0         1        0   
  2            2          1         0        0
  3            3          0         0       1

结果将以

生成
  question1   userid   answer3
     3           3         1

我怎么能用sql做到这一点?我尝试了很多,但没有成功,如果有人可以提供帮助,万事先感谢他......

这是我的SQL查询

SELECT * from questions Q WHERE  Q.userid=1 AND (Q.answer1=1 OR Q.answer2=1 OR Q.answer3=1)

5 个答案:

答案 0 :(得分:2)

试试这个

SELECT *
,CASE
    WHEN ( answer1 =1)   THEN 'answer1'
    WHEN ( answer2 =1)  THEN 'answer2'
    WHEN ( answer3 =1)   THEN ' answer3'
    ELSE 'nobody'
END
AS selected_answer
FROM Q
WHERE  Q.userid=1 AND (Q.answer1=1 OR Q.answer2=1 OR Q.answer3=1)

答案 1 :(得分:0)

首先必须将表格划分为4个,如下所示:

用户:id - 名称

问题:id - 文字

答案:id - fk_question - 文字

User_answers:id - fk_user - fk_question - fk_answer

我的出口:

CREATE TABLE IF NOT EXISTS `answer` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `fk_question` int(11) NOT NULL,
  `text` varchar(30) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;

INSERT INTO `answer` (`id`, `fk_question`, `text`) VALUES
(1, 1, 'it is 3'),
(2, 1, 'it is 2'),
(3, 1, 'it is 5');

CREATE TABLE IF NOT EXISTS `question` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `text` varchar(30) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

INSERT INTO `question` (`id`, `text`) VALUES
(1, 'what is 1 + 1');

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(30) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=11 ;

INSERT INTO `users` (`id`, `name`) VALUES
(1, 'pipo'),
(2, 'kees'),
(3, 'jan'),
(4, 'hessel'),
(5, 'geke'),
(6, 'daan'),
(7, 'ray'),
(8, 'jelle'),
(9, 'klaas'),
(10, 'berent');

CREATE TABLE IF NOT EXISTS `users_answers` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `fk_user` int(11) NOT NULL,
  `fk_question` int(11) NOT NULL,
  `fk_answer` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

INSERT INTO `users_answers` (`id`, `fk_user`, `fk_question`, `fk_answer`) VALUES
(1, 2, 1, 2),
(2, 4, 1, 3);

然后很容易,您不需要查看哪个列名设置为1,您只需获取数据。这种方式可以有3个以上的答案。事实上现在是它的变量:

SELECT q.`id` , u.`id` , a.`id`
FROM  `users_answers` ua
INNER JOIN  `users` u ON ua.`fk_user` = u.`id`
INNER JOIN  `question` q ON ua.`fk_question` = q.`id`
INNER JOIN  `answer` a ON ua.`fk_answer` = a.`id`
WHERE u.`id` =2

将返回

question    user    answer

1             2        2

但我会直接使用它:

SELECT q.`text` AS  `question` , u.`name` AS  `user` , a.`text` AS  `answer` 
FROM  `users_answers` ua
INNER JOIN  `users` u ON ua.`fk_user` = u.`id` 
INNER JOIN  `question` q ON ua.`fk_question` = q.`id` 
INNER JOIN  `answer` a ON ua.`fk_answer` = a.`id` 
WHERE u.`id` =2
AND q.`id` =1

它返回:

question        user    answer
what is 1 + 1   kees    it is 2

答案 2 :(得分:0)

如果要根据某些列是否包含在SQL中无法实现的值而仅返回某些列。你能够实现这一目标的唯一方法就是使用PIVOT,否则你将不得不带回select语句中建立的所有列。

答案 3 :(得分:-1)

我认为最简单的方法就是使用您在上面发布的查询获取数据并迭代结果并在客户端获取所需的列(在PHP中,在您的情况下)

答案 4 :(得分:-1)

SELECT * FROM question WHERE userid = 1 AND(answer1 = 1 || answer2 = 1 || answer3 = 1)

这会显示相应匹配的完整表格。此外,您可以单独获取您想要的记录,并与您的程序中的其他操作一起使用。