简体   繁体   中英

Select values from 2 different tables and print values in a row with the same ID

I have this database

TABLE "ADS"

ID   |   Visible
-----------------
15   |      1
16   |      1
17   |      1

TABLE "IMAGES_ADS"

ID    |    NAME    |   ID_ADS
------------------------------
100   |  xlasd.jpg |    15
101   |  dadsa.jpg |    15
102   |  dsfsf.jpg |    16
103   |  ghdfd.jpg |    17
104   |  jkyhg.jpg |    17
105   |  rerem.jpg |    17

Now I want create a php page that get values from this 2 tables and print a list of ADS by id with a row of the images name relate to the ADS ID separated by a comma.

I wrote this code

<?php
//ENTER YOUR DATABASE CONNECTION INFO BELOW:
$servername = "localhost";
$username = "user";
$password = "xxxxxx";
$dbname = "dbname";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} else {
    echo 'CONNECT TO DB';
}

$sql = "SELECT ADS.ID, IMAGES_ADS.NAME FROM ADS INNER JOIN IMAGES_ADS ON ADS.ID=IMAGES_ADS.ID_ADS WHERE ADS.Visible=1 ORDER BY ID ASC";
$results = $conn->query($sql);

echo '<table>';

if($results->num_rows > 0) {
    while($row = $results->fetch_assoc()) {
        echo '<tr>
                <td>'.$row["ID"].'</td>
                <td>'.$row["NAME"].'</td>
              </tr>';
    }
} else {
    echo '0 rows';
}
echo '</table>';

$conn->close();
?>

But with this code I get this result:

15  xlasd.jpg
15  dadsa.jpg
16  dsfsf.jpg
17  ghdfd.jpg
17  jkyhg.jpg
17  rerem.jpg

But I need this kind of result:

15  xlasd.jpg, dadsa.jpg
16  dsfsf.jpg
17  ghdfd.jpg, jkyhg.jpg, rerem.jpg

How can I have this result?

Thanks to @Vipin Jain I write the solution below:

<?php
//ENTER YOUR DATABASE CONNECTION INFO BELOW:
$servername = "localhost";
$username = "user";
$password = "xxxxxx";
$dbname = "dbname";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} else {
    echo 'CONNECT TO DB';
}

$sql = "SELECT ADS.ID, GROUP_CONCAT(IMAGES_ADS.NAME SEPARATOR', ') as b
        FROM ADS INNER JOIN IMAGES_ADS
        ON ADS.ID=IMAGES_ADS.ID_ADS
        WHERE ADS.Visible=1
        ORDER BY ID ASC";

$results = $conn->query($sql);

echo '<table>';

if($results->num_rows > 0) {
    while($row = $results->fetch_assoc()) {
        echo '<tr>
                <td>'.$row["ID"].'</td>
                <td>'.$row["b"].'</td>
              </tr>';
    }
} else {
    echo '0 rows';
}
echo '</table>';

$conn->close();
?>

You should use the GROUP_CONCAT

SELECT ADS.ID, GROUP_CONCAT(IMAGES_ADS.NAME)
FROM ADS 
INNER JOIN IMAGES_ADS 
ON ADS.ID=IMAGES_ADS.ID_ADS 
WHERE ADS.Visible=1 
GROUP BY ID 
ORDER BY ID ASC;

You can get by below query-

SELECT a.ID, GROUP_CONCAT(b.NAME)
FROM ADS as a
INNER JOIN IMAGES_ADS as b
ON a.ID=b.ID_ADS 
WHERE ADS.Visible=1 
Group by a.ID;
$res = [];    
while($row = $results->fetch_assoc()) {
    if (empty($res[$row["ID"]])) {
        $res[$row["ID"]] = [$row];
    } else {
        $res[$row["ID"]][] = $row;
    }
}

you'll get arrays of rows grouped by ID

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