简体   繁体   English

MySQL SELECT子查询多对多关系

[英]MySQL SELECT to subquery a many-to-many relationship

I have a bilingual dictionary database that I've created, and the tables are set up like so: 我已经创建了一个双语词典数据库,并且表的设置如下:

lemma (lemmaID, lemma, meaning)

collocate (collocateID, lemmaID, collocate, notes, connection)

collusage (usageID, lemmaID_u, collocateID_u, japanese, english, englishalt)

partofspeech (posID, partofspeech)

postolemma (lemmaID_p, posID_p)

So far, I have a query that returns tables for the results, and it works just how I'd like it to. 到目前为止,我有一个查询,该查询返回用于结果的表,并且它的工作方式与我想要的一样。 (It looks like this ) (看起来像这样

$q = 'SELECT *
FROM lemma, collocates, collusage
WHERE lemma.lemmaID = collocates.lemmaID AND lemma.lemmaID = collusage.lemmaID_u AND collusage.collocateID_u = collocates.collocateID
ORDER BY lemma.lemmaID;';

$result = mysqli_query($con, $q) or die(mysql_error());

if (!$result || mysqli_num_rows($result) == 0) {
    echo 'No rows found';
    exit;
}

$lastCatID = 0;

while ($row = mysqli_fetch_assoc($result)) {
    $reading = $row['reading'];
    $headword = $row['lemma'];

    $collocate = $row['collocate'];

    if (isset($row['notes'])) {
        $notes = '('.$row['notes'].')';
    } else {
        $notes = $row['notes'];
    }

    $japanese = $row['japanese'];
    $english = $row['english'];

    if (isset($row['englishalt'])) {
        $englishalt = ', '.$row['englishalt'].'';
    } else {
        $englishalt = $row['englishalt'];
    }

    if ($lastCatID != $row['lemmaID']) {
        //starting a new category
        if ($lastCatID != 0) {
            //close up previous table
            echo '    </tbody>
            </table> </div>';
        }
        //start a new div
                echo '<div class="entry">
                <h4>'.$reading.'【'.$headword.'】 <span class="pos">'.$WANT TO LIST PARTS OF SPEECH HERE.'</span></h4>
                <table class="table table-striped table-hover">

                    <tbody>';
        $lastCatID = $row['lemmaID'];
    }

    echo '<tr>
            <td><span>'.$collocate.'</span><span class="notes">'.$notes.'</span></td>
            <td>'.$japanese.'</td>
            <td>'.$english.''.$englishalt.'</td>
            </tr>';
}

        if ($lastCatID != 0) {
            //close up the final table
            echo '    </tbody>
            </table></div>';
        }

        mysqli_free_result($result);

What I can't figure out how to do is to use the postolemma junction table to get all of the partofspeech values for each lemmaID so I can list them next to the lemma in the table. 我不知道该怎么做,是使用postolemma联结表来获取每个lemmaID的所有词性值,因此我可以在表中的lemma旁边列出它们。 Everything SELECT query I have done so far has duplicated collocation entries, which I don't want. 到目前为止,我所做的所有SELECT查询都具有重复的并置条目,我不希望这样做。 Any help is appreciated! 任何帮助表示赞赏!

Edit: Here is a link to the SQL Fiddle with data. 编辑:这是带有数据的SQL Fiddle的链接。 I couldn't get my foreign key constraints to work so just that is missing. 我无法使我的外键约束起作用,所以只是缺少了。

if I understand you correctly you want to select all values from table partofspeech based on lemma table. 如果我对您的理解正确,那么您希望基于引理表从表词性中选择所有值。 Your query should look like this: 您的查询应如下所示:

SELECT part.partofspeech 
FROM partofspeech part
INNER JOIN postolemma post
    ON part.posID = post.posID_p
INNER JOIN lemma l
    ON post.lemmaID_p = l.lemmaID

Also I would suggest you to change the query you use and start using JOIN operator in syntax, it's good practice and it's not hard to switch from one to another... So your query: 我也建议您更改所使用的查询,并开始在语法中使用JOIN运算符,这是一个好习惯,并且不难从一个切换到另一个...因此,您的查询:

SELECT *
FROM lemma, collocates, collusage
WHERE lemma.lemmaID = collocates.lemmaID 
      AND lemma.lemmaID = collusage.lemmaID_u 
      AND collusage.collocateID_u = collocates.collocateID
ORDER BY lemma.lemmaID;

Will look like this: 将如下所示:

 SELECT * 
 FROM lemma
 INNER JOIN collocates
     ON lemma.lemmaID = collocates.lemmaID
 INNER JOIN collusage
    ON collusage.collocateID_u = collocates.collocateID
    AND lemma.lemmaID = collusage.lemmaID_u
 ORDER BY lemma.lemmaID;

Also you can use aliases for table in you query like i do in first query I wrote here. 您也可以在查询中为表使用别名,就像我在此处编写的第一个查询中一样。 It will make your life easier because you don't need to type whole name of table over and over... 这将使您的生活更加轻松,因为您无需一遍又一遍地键入表格的全名...

GL! GL!

PS also it's good to post your desired result in you question and provide SQL Fiddle with some data for our better understanding of your queston... PS还可以将您想要的结果发布到您的问题中,并为SQL Fiddle提供一些数据,以更好地了解您的问题,这也是很好的...

EDIT 编辑

After we consulted in the comments we come to this solution: 在咨询了评论之后,我们得出了这个解决方案:

 SELECT * 
 FROM lemma
 INNER JOIN collocates
     ON lemma.lemmaID = collocates.lemmaID
 INNER JOIN collusage
    ON collusage.collocateID_u = collocates.collocateID
    AND lemma.lemmaID = collusage.lemmaID_u
 INNER JOIN (SELECT post.lemmaID_p AS lemmaID, group_concat(part.partofspeech SEPARATOR ', ') AS partofspeach
             FROM partofspeech part
             INNER JOIN postolemma post
               ON part.posID = post.posID_p
             INNER JOIN lemma l
               ON post.lemmaID_p = l.lemmaID
             GROUP BY post.lemmaID_p) tmp
 ON lemma.lemmaID = tmp.lemmaID
 ORDER BY lemma.lemmaID;

Here is SQL Fiddle for that... 这是SQL Fiddle ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM