简体   繁体   中英

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. 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!

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. 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".

What you call DISTINCT can only be applied [to the results you get] after the itemization .

After applying the DISTINCT, you then need to re-group. 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:

$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/>";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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