内联接查询从数据库中选择,其中lang是en

时间:2016-05-31 18:39:44

标签: php mysql

你好我有一个内部联接的查询,它从数据库中获取问题和4个答案。我想让这个查询只考虑lang table是'en'的问题 这是查询:

$mysql->query("SELECT Q.id AS id, Q.question, QA.answer1, QA.answer2,
                      QA.answer3, QA.answer4, QA.correct, QC.name AS cat_name 
               FROM question Q
                   INNER JOIN question_answers QA ON QA.questionFK=Q.id
                   INNER JOIN question_cats QC ON QC.id=Q.categoryFK
               ORDER BY rand()
               LIMIT 1");

我试着这样做:

$mysql->query("SELECT Q.id AS id, Q.question, QA.answer1, QA.answer2, 
                      QA.answer3, QA.answer4, QA.correct, QC.name AS cat_name 
                FROM question Q
                   INNER JOIN question_answers QA ON QA.questionFK=Q.id
                   INNER JOIN question_cats QC ON QC.id=Q.categoryFK
                WHERE Q.lang='en'
                ORDER BY rand()
                LIMIT 1");

但它不起作用,它选择了每一个...... 我哪里错了,我该怎么做?

这是两个表:

CREATE TABLE IF NOT EXISTS `question` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `question` varchar(255) NOT NULL,
  `cnt` int(10) NOT NULL DEFAULT '0',
  `correct` int(10) NOT NULL DEFAULT '0',
  `categoryFK` int(3) NOT NULL DEFAULT '0',
  `from_userFK` int(10) NOT NULL DEFAULT '0',
  `correct_points` int(10) DEFAULT NULL,
  `ut` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `del` enum('yes','no') NOT NULL DEFAULT 'no',
  `lang` varchar(32) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=26 ;

CREATE TABLE IF NOT EXISTS `question_answers` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `questionFK` int(10) NOT NULL,
  `answer1` varchar(255) CHARACTER SET cp1251 NOT NULL,
  `answer2` varchar(255) CHARACTER SET cp1251 NOT NULL,
  `answer3` varchar(255) CHARACTER SET cp1251 NOT NULL,
  `answer4` varchar(255) CHARACTER SET cp1251 NOT NULL,
  `correct` int(1) NOT NULL,
  `ut` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `del` enum('yes','no') CHARACTER SET cp1251 NOT NULL DEFAULT 'no',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=25 ;

1 个答案:

答案 0 :(得分:0)

对我而言,感觉你的档案条件WHERE Q.lang='en'并不匹配任何记录,因此结果如此。在比较之前考虑修剪它

            WHERE TRIM(Q.lang) ='en'