2011-01-28 105 views
1

我有两个表:选择同一列中有两个不同的领域在一个查询,

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返回的查询结果为空:(

我希望获得使用sectionssection_name = '指数' 和textstext_language = 'EN':。

section_title = 'Home' 
section_description = 'Welcome' 

或使用sectionssection_name ='联系'和textstext_language =“上课”:

section_title = 'Contacto' 
section_description = 'Contactenos' 
+0

+1对于有用的DDL – 2011-01-28 00:28:04

+0

你的SELECT是一款列,但你列出案文值预期输出 - 这是正确的? – 2011-01-28 00:40:33

+0

文本值与预期的输出 – quantme 2011-01-28 00:43:33

回答

1

你需要join两次......这样的:

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' 
1

我编辑了上面的查询了一下,但要低信誉做出评论= 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' 
相关问题