选择仅包含一个不同列的重复行并将其分组

时间:2017-09-24 13:25:57

标签: php html mysql sql

SELECT
    a.FirstName,
    a.LastName,
    c.Country,
    SUM(b.Scores) AS Score
FROM names a
INNER JOIN countries c
    ON a.id = c.cid
INNER JOIN scores b
    ON a.id = b.sid
WHERE a.id = :id
GROUP BY c.Country
ORDER BY Score ASC

我正在尝试将此查询用于selectduplicated,但countries行与 names countries scores [id -- FirstName -- LastName] [id -- cid -- Country] [id -- sid -- Score] [1 -- FName1 -- LName1 ] [1 -- 1 -- USA ] [1 -- 1 -- 5] [2 -- FName2 -- LName2 ] [2 -- 1 -- France ] [2 -- 1 -- 6] [3 -- FName3 -- LName3 ] [3 -- 1 -- Germany] [3 -- 2 -- 7] [4 -- FName4 -- LName4 ] [4 -- 2 -- Germany] [4 -- 2 -- 8] [5 -- 2 -- Czech ] [5 -- 2 -- 2] [6 -- 3 -- Germany] [6 -- 4 -- 10] [7 -- 4 -- France ] [7 -- 3 -- 15] 不同

{id[1] -> FName1 -> LName1}--{cid[1] -> USA, France, Germany}--{sid[1] -> 11}
{id[2] -> FName2 -> LName2}--{cid[2] -> Czech    ,   Germany}--{sid[2] -> 17}
{id[3] -> FName3 -> LName3}--{cid[3] ->        Germany      }--{sid[3] -> 10}
{id[4] -> FName4 -> LName4}--{cid[4] ->        France       }--{sid[4] -> 15}

静态应该是

.Container div {
    border: 1px solid blue;
    margin: 10px;
}
.Container div div {
    border: 1px solid red;
    margin: 10px;
}

我想获取这些数据,如以下HTML结构

<div>
    <div id="Master German">German</div>
    <div id="German">
        <div class="Container">
           <div id='FName2 German'>
                <div>FName2</div>
                <div>LName2</div>
            </div>
            <div id='FName1 German'>
                <div>FName1</div>
                <div>LName1</div>
            </div>
            <div id='FName3 German'>
                <div>FName3</div>
                <div>LName3</div>
            </div>
        </div>
    </div>

    <div id="Master France">France</div>
    <div id="France">
        <div class="Container">
           <div id='FName4 France'>
                <div>FName4</div>
                <div>LName4</div>
            </div>
            <div id='FName1 France'>
                <div>FName1</div>
                <div>LName1</div>
            </div>
        </div>
    </div>

   <div id="Master Czech">Czech</div>
    <div id="Czech">
        <div class="Container">
            <div id='FName2 Czech'>
                <div>FName2</div>
                <div>LName2</div>
            </div>
        </div>
    </div>
    
    <div id="Master USA">USA</div>
    <div id="USA">
        <div class="Container">
            <div id='FName1 USA'>
                <div>FName1</div>
                <div>LName1</div>
            </div>
        </div>
    </div>
</div>
PHP

我在查询中尝试使用的是以下$pre = null; echo "<div>"; $end = ""; while ($row = $stmt->fetch()) { $FName = $row['FirstName']; $LName = $row['LastName']; $Country = $row['Country']; if ($Country != $pre) { echo $end; echo "<div id='Master $Country'>$Country</div>"; echo "<div id='$Country'>"; } $end = "</div> </div>"; $pre = $Country; echo " <div id='$FName USA'> <div>$FName</div> <div>$LName</div> </div>"; } echo $end; echo "</div>"; 代码

<button id="whatever" class="donotcare" (click)="doSomething($event)">blabla bla</div>

但是根本无法正常工作并且让我头疼,试图解决它,但没有希望。

有没有更好的方法来解决这个问题?从SQL端或PHP端或尽可能混合。

1 个答案:

答案 0 :(得分:1)

通过&#34;行&#34;,我将您的问题解释为names有多个委员。如果是这样的话:

SELECT n.FirstName, n.LastName,
       GROUP_CONCAT(DISTINCT c.Country) as countries, SUM(s.Scores) AS Score
FROM names n INNER JOIN
     countries c
     ON n.id = c.cid INNER JOIN
     scores s
     ON n.id = s.sid
WHERE n.id = :id
GROUP BY n.id, n.FirstName, n.LastName
HAVING COUNT(DISTINCT c.country) > 1
ORDER BY Score ASC;