简体   繁体   English

如何同时使用DISTINCT行和非DISTINCT行

[英]How to both use a DISTINCT row and a non DISTINCT row

all! 所有! I have a bit of a tricky one for you today, I want to use the select DISTINCT statement to both select a row that needs to be distinct but also in the same statement (or the way I a have tried?) a row that doesn't/can't be distinct. 我今天有一个棘手的问题,我想使用select DISTINCT语句既选择需要区分的行,又选择同一条语句(或我尝试过的方式?) 't /无法区分。 My desired result is to only have one of each of the classnames. 我想要的结果是每个类名只有一个。 Currently it outputs like this: 当前它的输出是这样的:

English: textbook, folder, laptop
English: textbook
Media:   textbook, folder
English: textbook, folder
English: textbook, folder
Art:     textbook

And this is how I want it to output: 这就是我希望它输出的方式:

English: textbook, folder, laptop
Media:   textbook, folder
Art:     textbook

This is the layout of the database: 这是数据库的布局:

|ID|classname|Book  
|49|English  |textbook, folder, laptop
|50|English  |textbook  
|53|Media    |textbook, folder 
|54|English  |textbook, folder 
|55|Art      |folder 

I'm obviously VERY new to php so any help would be appreciated! 我显然是php的新手,因此不胜感激!

This is my approach so far: 到目前为止,这是我的方法:

$sql = "SELECT DISTINCT classname FROM classes ORDER BY Due;";
$result1 = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result1);
if ($resultCheck > 0){
    while ($row = mysqli_fetch_assoc($result1)){
        $classname = $row["classname"];

        if ($classname == "English"){
            $newName = $classname;
            $sql = "SELECT Book FROM classes WHERE Book='$newName';";
            $result1 = mysqli_query($conn, $sql);
            $resultCheck = mysqli_num_rows($result1);

            if ($resultCheck > 0){
                while ($row = mysqli_fetch_assoc($result1)){
                    $materials = $row["Book"]; 
                    echo "<div class='subname'>$newName:";
                    echo "<div class='wow'>$materials</div>";
                    echo "</div><br>";
                }

            }
        }
    }
}

Well, given the crappy design of your database the first step you need to do is itemize your lists such as "textbook, folder, laptop" [such that you get three rows with one item instead of one row with three items]. 好吧,考虑到数据库笨拙的设计,您需要做的第一步是逐项列出诸如“教科书,文件夹,笔记本电脑”之类的列表(这样一来,每行只有三行,而不是三行)。 The semantics of that operation is a bit like SQL UNNEST but sadly, the "structure" (insofar as using that word is appropriate for what you have) of your database is unfit for using that. 该操作的语义有点像SQL UNNEST,但可悲的是,数据库的“结构”(因使用该词而定,适合于您所拥有的)不适合使用该结构。 I'm not sure it can be done without some form of procedural coding, so the answer most likely to be correct is "just forget doing that in one SQL statement". 我不确定没有某种形式的过程编码就可以做到,因此最有可能正确的答案是“只是忘了在一个SQL语句中这样做”。

What you call DISTINCT can only be applied [to the results you get] after the itemization . 您所谓的DISTINCT 只能 在逐项列出之后 应用于 [获得的结果]。

After applying the DISTINCT, you then need to re-group. 应用DISTINCT之后,您需要重新分组。 Maybe it can be done in client-side languages, but those are not my cup of tea. 也许可以用客户端语言来完成,但是那不是我要的。

As a general statement, this isn't a very robust database design. 一般而言,这不是一个非常可靠的数据库设计。 You may be better served to normalize your table and have a single classname-book combination in every row. 您可以更好地规范化表,并在每行中使用一个类名-书的组合。

If that is not a possibility, I'd group_concat all the books per class and then explode them to an array on the PHP side, make the result unique, and join it back to a string: 如果这不是一个可能性,我会group_concat每类中的所有书籍,然后explode他们在PHP端阵列,使结果独特,并加入回一个字符串:

$sql = "SELECT classname, GROUP_CONCAT(book SEPARATOR ', ') AS materials FROM classes GROUP BY classname";
$result = mysqli_query($conn, $sql);

while ($row = mysqli_fetch_assoc($result)) {
    $classname = $row["classname"]; 
    $materials = $row["materials"];
    $materials = implode(',', array_unique(explode(', ', $materials)));
    echo "<div class='subname'>${classname}:";
    echo "<div class='wow'>$materials</div>";
    echo "</div><br/>";
}

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

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