在MySQL

时间:2017-07-11 13:19:02

标签: mysql sql join

我阅读了问题Combine multiple rows into one row MySQL,该问题展示了如何使用单行从 SELECT 语句的多行中获取结果。 SQL Fiddle with Demo也运行正常。在该问题中,有两个表: t1 t2 。我的问题有三个表,我已经简化了这个问题:

语言

ID              INT(11)         NOT NULL    PRIMARY_KEY
name            VARCHAR(255)    NOT NULL    UNIQUE

languages_have_persons

Languages_ID    INT(11)         NOT NULL    PRIMARY_KEY
Persons_ID  INT(11)             NOT NULL    

ID              INT(11)         NOT NULL    PRIMARY_KEY
firstName       VARCHAR(255)    NOT NULL
lastName        VARCHAR(255)    NOT NULL

我想要做的是将这些表类似地组合起来,但是我现在几个小时都没有这样做。这是我的最后一次尝试:

SELECT p1.ID
    , p1.firstName
    , p1.lastName
    CONCAT_WS(', '
        , l2de.name
        , l2en.name
    )
    FROM
        languages_have_persons AS lp, persons AS p1
    LEFT JOIN languages AS l2de ON
        l2de.ID = 4 -- German
    LEFT JOIN languages AS l2en ON
        l2en.ID = 5 -- English
WHERE p1.ID = lp.Persons_ID
ORDER BY lastName, firstName;

错误消息显示为:

Fehler
SQL-Befehl: Dokumentation


SELECT p1.ID AS ID
    , p1.firstName AS firstName
    , p1.lastName AS lastName
    CONCAT_WS(', '
        , l2de.name
        , l2en.name
    )
    FROM
        languages_have_persons AS lp, persons AS p1
    LEFT JOIN languages AS l2de ON
        l2de.ID = 4     LEFT JOIN languages AS l2en ON
        l2en.ID = 5 WHERE p1.ID = lp.Persons_ID
ORDER BY lastName, firstName LIMIT 0, 25
MySQL meldet: Dokumentation

#1064 - Fehler in der SQL-Syntax. Bitte die korrekte Syntax im Handbuch
nachschlagen bei '(', '
        , l2de.name
        , l2en.name
    )
    FROM
        languages_have_persons AS lp, ' in Zeile 4

我也尝试过其他方法,但没有一种方法具有正确的语法。我想要的结果如下:

语言

ID    name
4     German
5     English

language_have_persons

Languages_ID    Persons_ID    
    4                2
    5                1
    5                2

ID    firstName    lastName
1     Joe           Frazier
2     Arnold        Schwarzenegger

我对正确的SELECT语句的期望是

firstName    lastName           CONCAT_WS(…
Joe           Frazier           English
Arnold        Schwarzenegger    German, English

您是否在上面的SQL语法中看到错误?如果没有,你知道如何解决它吗?

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

SELECT p1.ID
    , p1.firstName
    , p1.lastName,<--------------------I had made change at this line
    CONCAT_WS(', '
        , l2de.name
        , l2en.name
    )
    FROM
        languages_have_persons AS lp, persons AS p1
    LEFT JOIN languages AS l2de ON
        l2de.ID = 4 -- German
    LEFT JOIN languages AS l2en ON
        l2en.ID = 5 -- English
WHERE p1.ID = lp.Persons_ID
ORDER BY lastName, firstName;

或者,您可以在下面尝试进一步查询。

SELECT p1.ID
    , p1.firstName
    , p1.lastName,
    l2de.name
    FROM
    persons p1
    INNER JOIN languages_have_persons lp on p1.ID=lp.Persons_ID 
    INNER JOIN language l2de on l2de.ID=lp.Languages_ID 
    WHERE l2de.ID  IN(4,5)
    ORDER BY lastName, firstName;

您可以尝试以上查询。

答案 1 :(得分:0)

在另一个网站上,我进行了另一次讨论,同事找到了解决方案,即

SELECT DISTINCT pl.ID
    , firstName
    , lastName
    , languages
FROM persons p
    , ( SELECT
        lp.Persons_ID AS ID,
        GROUP_CONCAT(DISTINCT l.name) AS languages
    FROM languages l
        , languages_have_persons lp
    WHERE l.id = lp.Languages_ID
    GROUP BY lp.Persons_ID
    ) AS pl
WHERE p.id = pl.id
ORDER BY p.id;

所以,问题是正确的答案。