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

发布于 2024-10-14 20:02:24 字数 1560 浏览 2 评论 0原文

我有两个表:

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_name = 'index' 和 texts.text_language = 'en':

section_title = 'Home'
section_description = 'Welcome'

或使用 sections.section_name = '联系人”和文本text_language = 'es':

section_title = 'Contacto'
section_description = 'Contactenos'

I have two tables:

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);

I'm newbie with queries and I don't know how to do next:

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 returned an empty result set :(

I expect to obtain using sections.section_name = 'index' and texts.text_language = 'en':

section_title = 'Home'
section_description = 'Welcome'

or using sections.section_name = 'contact' and texts.text_language = 'es':

section_title = 'Contacto'
section_description = 'Contactenos'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桃酥萝莉 2024-10-21 20:02:24

您需要加入两次...如下所示:

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'

You'll need to join twice... like this:

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'
信仰 2024-10-21 20:02:24

我对上面的查询进行了一些编辑,但必须以较低的声誉发表评论 =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'

I edited the above query a little, but has to low reputation to make a comment =P Try again (works for me):

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'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文