Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 在一个查询中从同一列中选择两个不同的字段,_Mysql_Select_Join_Field - Fatal编程技术网

Mysql 在一个查询中从同一列中选择两个不同的字段,

Mysql 在一个查询中从同一列中选择两个不同的字段,,mysql,select,join,field,Mysql,Select,Join,Field,我有两张桌子: CREATE TABLE sections (id int, section_name varchar(16), section_title int, section_description int); INSERT INTO sections VALUES(1, 'index', 1, 2); INSERT INTO sections VALUES(2, 'contact', 3, 4); CREATE TABLE texts (id int, text_value varc

我有两张桌子:

CREATE TABLE sections (id int, section_name varchar(16), section_title int, section_description int);
INSERT INTO sections VALUES(1, 'index', 1, 2);
INSERT INTO sections VALUES(2, 'contact', 3, 4);

CREATE TABLE texts (id int, text_value varchar(64), text_language varchar(2), text_link int);
INSERT INTO texts VALUES(1, 'Home', 'en', 1);
INSERT INTO texts VALUES(2, 'Inicio', 'es', 1);
INSERT INTO texts VALUES(3, 'Welcome', 'en', 2);
INSERT INTO texts VALUES(4, 'Bienvenidos', 'es', 2);
INSERT INTO texts VALUES(5, 'Contact', 'en', 3);
INSERT INTO texts VALUES(6, 'Contacto', 'es', 3);
INSERT INTO texts VALUES(7, 'Contact Us', 'en', 4);
INSERT INTO texts VALUES(8, 'Contactenos', 'es', 4);
我是查询新手,不知道下一步该怎么做:

SELECT `sections`.`section_title`
     , `sections`.`section_description`
FROM `sections`
    INNER JOIN `texts`
    ON (`sections`.`section_title` = `texts`.`text_link`) AND (`sections`.`section_description` = `texts`.`text_link`)
    WHERE `sections`.`section_name` = 'index' AND `texts`.`text_language` = 'en'
;
MySQL返回了一个空结果集:

我希望使用sections.section\u name='index'和text.text\u language='en'获得:

或者使用sections.section\u name='contact'和text.text\u language='es':

你需要加入两次。。。像这样:

SELECT
  t1.text_value AS section_title,
  t2.text_value AS section_description
FROM `sections`
  INNER JOIN `texts` AS t1
    ON (`sections`.`section_title` = t1.`text_link`)
  INNER JOIN `texts` AS t2
    ON (`sections`.`section_description` = t2.`text_link`)
WHERE `section_name` = 'index'
    AND t1.`text_language` = 'en'
    AND t2.`text_language` = 'en'

我对上面的查询进行了一些编辑,但为了发表评论不得不降低声誉=p重试对我有效:

SELECT t1.text_value AS section_title, 
       t2.text_value AS section_description
  FROM `sections` AS s
INNER JOIN `texts` as t1 ON (s.`section_title` = t1.`text_link`)
INNER JOIN `texts` as t2 ON (s.`section_description` = t2.`text_link`)
     WHERE s.`section_name` = 'index' 
       AND t1.`text_language` = 'en' 
       AND t2.`text_language` = 'en'

您选择的是节列,但您将文本值列为预期输出-这是正确的?文本值列为预期输出我从未见过像那个新手那样的代码,我尝试过,但返回错误:1054-未知列“t1.section_title”在“field list”@OMG Ponies:Query:s错误代码:1064您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以了解在第1行使用接近“s”的正确语法总计时间:00:00:00:983我修改了您给我的现在编辑的代码,现在它可以工作了!谢谢你加入一个lotuses而不是sections
SELECT
  t1.text_value AS section_title,
  t2.text_value AS section_description
FROM `sections`
  INNER JOIN `texts` AS t1
    ON (`sections`.`section_title` = t1.`text_link`)
  INNER JOIN `texts` AS t2
    ON (`sections`.`section_description` = t2.`text_link`)
WHERE `section_name` = 'index'
    AND t1.`text_language` = 'en'
    AND t2.`text_language` = 'en'
SELECT t1.text_value AS section_title, 
       t2.text_value AS section_description
  FROM `sections` AS s
INNER JOIN `texts` as t1 ON (s.`section_title` = t1.`text_link`)
INNER JOIN `texts` as t2 ON (s.`section_description` = t2.`text_link`)
     WHERE s.`section_name` = 'index' 
       AND t1.`text_language` = 'en' 
       AND t2.`text_language` = 'en'