Click here to Skip to main content
15,888,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Cheers,

I want to concat multiple rows to one column

Right now, my output is

CaseID   Name        Number    Register date    filename    Actions 
2        John Doe    3/2020    2020-01-20       test1.pdf    delete
2        John Doe    3/2020    2020-01-20       test2.pdf    delete
3        Jane Doe    5/2020    2020-01-20       test3.pdf    delete

I want something like this:

CaseID   Name        Number    Register date    filename    Actions 
    2    John Doe    3/2020     2020-01-20      test1.pdf    delete
                                                test2.pdf

    3    Jane Doe    5/2020    2020-01-20       test3.pdf    delete

All-day I have tried different SQL statements, but nothing works. I'm stuck, I have no idea how to resolve it because I need to use LEFT JOIN for 2 tables:

$sql = "SELECT * FROM files as f
         LEFT JOIN cases AS c on f.id_case_f = c.id_case
         LEFT JOIN customers as cs on c.id_customer = cs.id_cust
         WHERE case_number LIKE '%$case_number%'";
$result = $db -> query($sql);


What I have tried:

I have tried with GROUP_CONCAT I have used the statement below, but it doesn't work.

    <pre><?php
$db = mysqli_connect("localhost", "root", "", "testdb");
if ($db -> connect_error){

    if($_REQUEST['submit']){
        $case_number = $_POST['case_number'];


        if(empty($case_number)){
           $make = '<h4>You must type a word to search!</h4>';
       }else{
           $make = '<h4></h4>';
           $sql = "SELECT id_case_f, GROUP_CONCAT(filename SEPARATOR ', ') FROM files
           LEFT JOIN cases AS c on f.id_case_f = c.id_case
           LEFT JOIN customers as cs on c.id_customer = cs.id_cust
           WHERE case_number LIKE '%$case_number%'
           GROUP BY id_case_f;";

           $result = $db -> query($sql);

           if($make = mysqli_num_rows($result) > 0)
               { echo '<table>
           <tr><th>CaseID</th>
           <th>Name </th>
           <th>Number </th>
           <th>Register date</th>
           <th>filename</th>
           <th>Actions</th></tr>';

           while($row = mysqli_fetch_assoc($result))
           {
              echo "<tr><td>". $row["id_case"] ." </td><td>". $row["name"] . " </td>
              <td>". $row["number"] ."</td> <td> "  . $row["register_date"] ." </td>
              <td> ". $row["filename"] ." </td><td><a href=#>delete</a></td></tr> ";
          }
          echo "</table>";
      }
      else
      {
        echo'<table "><tr><th>Try again! </th></tr></table>';

        print ($make);
    }
    mysqli_free_result($result);
    mysqli_close($db);
}
}
?>

Also, I have used a loooot of other combinations, but unfortunately, nothing worked. :(

If it is necessary I can add the entire PHP code, but this issue is regarding the select statement

Can someone help me with this? Thank you

Errors:

Notice: Undefined index: id_case in /// on line 196
Notice: Undefined index: name
Notice: Undefined index: number
Notice: Undefined index: register_date
Notice: Undefined index: filename
Posted
Updated 20-Jan-20 5:46am

1 solution

You could try something like
SQL
SELECT cs.*,
       c.*,
       (SELECT GROUP_CONCAT(f2.filename ORDER BY f2.filename SEPARATOR ', ') 
        FROM files f2
        WHERE f2.id_case_f = c.id_case) AS filenames
FROM cases AS c 
LEFT JOIN customers as cs on c.id_customer = cs.id_cust
WHERE EXISTS (SELECT 1 
              FROM files f
              WHERE f.id_case_f = c.id_case
AND   case_number LIKE '%$case_number%'
 
Share this answer
 
Comments
Member 14721629 20-Jan-20 12:08pm    
Definitely worked. Thank so much. You saved me!!
Wendelius 20-Jan-20 12:18pm    
You're welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900