Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Bagaimana hasil gabungan keluaran dari dua tabel MySQL menggunakan PDO

Anda dapat menggabungkan peserta menggunakan GROUP_CONCAT

SELECT 
    conf.conference_sid,
    date_created, 
    TIMEDIFF(date_completed, date_created) AS duration,
    conf.RecordURL, 
    conf.PIN,
    GROUP_CONCAT(pid SEPARATOR ",") AS pid,
    GROUP_CONCAT(call_sid SEPARATOR ",") AS call_sid,
    GROUP_CONCAT(call_from SEPARATOR ",") AS call_from,
    GROUP_CONCAT(name_recording SEPARATOR ",") AS name_recording
FROM 
    conference conf
LEFT OUTER JOIN 
    participants p ON p.conference_sid = conf.conference_sid
WHERE 
    conf.PIN = 123
GROUP BY conf.conference_sid

Lihat SQLFIDDLE dan dokumentasi MySQL tentang TIMEDIFF .

Sekarang logika aplikasi akan menjadi

<?php
  $pin = 123;
  $db = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');
  $stmt = $db->prepare(
    'SELECT 
        conf.conference_sid,
        date_created, 
        timediff(date_completed, date_created) AS duration,
        conf.RecordURL, 
        conf.PIN,
        GROUP_CONCAT(pid SEPARATOR ",") AS pid,
        GROUP_CONCAT(call_sid SEPARATOR ",") AS call_sid,
        GROUP_CONCAT(call_from SEPARATOR ",") AS call_from,
        GROUP_CONCAT(name_recording SEPARATOR ",") AS name_recording
    FROM 
      conference conf
    LEFT OUTER JOIN 
      participants p ON p.conference_sid = conf.conference_sid
    WHERE 
      conf.PIN = :pin
    GROUP BY conf.conference_sid');
  $stmt->bindParam(':pin', $pin);
?>
<table border="1">
<thead>
  <th>Date</th>
  <th>Duration</th>
  <th>Participants</th>
  <th>Recording</th>
</thead>

<tbody>
<?php
  $stmt->execute();
  while ($row = $stmt->fetch()) {
?>
    <tr>
      <td><?php echo $row['date_created']; ?></td>
      <td><?php echo $row['duration']; ?></td>
      <td>
        <table border="1">
          <thead>
            <th>call_sid</th>
            <th>call_from</th>
            <th>name_recording</th>
          </thead>
          <tbody>

<?php
    $length = count(explode(',', $row['pid']));
    $call_sid = explode(',', $row['call_sid']);
    $call_from = explode(',', $row['call_from']);
    $name_recording = explode(',', $row['name_recording']);
    for ($i=0; $i < $length; $i++) { 
      ?>
        <tr>
          <td> <?php echo $call_sid[$i]; ?> </td>
          <td> <?php echo $call_from[$i]; ?></td>
          <td> <?php echo $name_recording[$i]; ?> </td>
        <tr>
<?php
    }
?>
        </tbody>
        </table>
      </td>
      <td>
        <a href="<?php echo $row['RecordURL']; ?>">
        Call recording</a>
      </td>
    </tr>
<?php
  }
?>
</tbody>

Anda akan mendapatkan hasil yang ditetapkan dengan nilai yang dipisahkan koma() dalam pid, call_sid, call_from, dan name_recording. Anda dapat mengonversi string ini menjadi array menggunakan explode .

array explode ( string $delimiter , string $string [, int $limit ] )


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Ratakan data anak/orang tua dengan jumlah kolom yang tidak diketahui

  2. Cara mengoptimalkan ORDER BY untuk kolom yang dihitung pada tabel MySQL MASSIVE

  3. BERBEDA kolom dalam database

  4. Bisakah Anda mengakses nilai kenaikan otomatis di MySQL dalam satu pernyataan?

  5. PHP + MySql + Prosedur Tersimpan, bagaimana cara mendapatkan akses nilai keluar?