显示来自mysql表的所有数据以及第二个表中的附加信息

时间:2016-02-25 06:30:50

标签: php html mysql

我在mysql db.Table_1和Table_2中有两个表我使用以下代码在HTML中显示整个表:

<table>
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "database";
$results = null;
try {
     $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     $stmt = $conn->prepare("SELECT * FROM Table_1"); 
     $stmt->execute();
     $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 
     $results = $stmt->fetchAll();
}
catch(PDOException $e) {
     echo "Error: " . $e->getMessage();
}
$conn = null;
?> 
<table>
      <thead>
        <tr>
          <th>Name</th>
          <th>Address</th>
          <th>Email</th>
        </tr>
      </thead>
      <tbody>
         <?php foreach($results as $key=>$row) { ?>
        <tr>
      <td><?php echo $row['Name'];?></td>
      <td><?php echo $row['Address'];?></td>
      <td><?php echo $row['Email'];?></td>
    </tr>
     <?php } ?>
       </tbody>
</table>
</table>

现在我想再向这个HTML表添加一列,其中的数据将来自Table_2。在这个Table_2中,将为Table_1中的单个帖子插入多个注释。

我使用查询语句查询帖子的最新评论:

SELECT comment FROM Table_2 WHERE id = (SELECT id 
FROM Table_2 WHERE post_id = 10 ORDER BY id DESC LIMIT 1)

这里我们指定post_id = 10但是当我使用SELECT * FROM Table_1“来显示table_1中的所有数据时,我应该如何指定在table_2中显示该特定帖子的最新评论

表-1结构:

+-------+---------+---------+---------+
|  id   |  Name   | Address | Email   |
+-------+---------+---------+---------+
|   1   |   ABC   |   ABC   |   ABC   |
|   2   |   DEF   |   DEF   |   DEF   |
+-------+---------+---------+---------+

表-2结构:

+-------+-------------+---------+
|  id   |  Table_1_id | Comments|
+-------+-------------+---------+
|   1   |     1       |   X     | 
|   2   |     1       |   Y     | 
|   3   |     2       |   Z     | 
+-------+-------------+---------+

要显示的HTML:

+---------+---------+---------+---------+
|  Name   | Address | Email   | Comments|
+---------+---------+---------+---------+
|   ABC   |   ABC   |   ABC   |    Y    |
|   DEF   |   DEF   |   DEF   |    Z    | 
+---------+---------+---------+---------+

提前致谢。

3 个答案:

答案 0 :(得分:1)

如果您想获得所有帖子及其最新评论,那么您可以使用以下查询:

public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
   Fragment fragment = null;
    int id = item.getItemId();
    String get_title = item.getTitle().toString();

    if (id == R.id.nav_quick_list) {

        fragment = new CreateQuickList();

    } else if (id == R.id.nav_my_account) {
        fragment = new MyAccount();

    }


    if (fragment != null) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.content_frame, fragment);
        ft.commit();
    }

    title_tv.setText(get_title);
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

它将给出如下输出结果:

SELECT 
Name,
Address,
Email,
finalTable.comment
FROM 
Table_1
INNER JOIN 
(

        SELECT 
        comment,
        Table_1_id
        FROM Table_2
        INNER JOIN 
        (   SELECT 
             MAX(id) latest_comment_id
            FROM Table_2
            GROUP BY Table_1_id ) latestComments
        ON Table_2.id = latestComments.latest_comment_id
) finalTable
ON finalTable.Table_1_id = Table_1.id;

注意:如果您想要所有帖子,我的意思是那些也没有任何评论的帖子,请将+---------+---------+---------+---------+ | Name | Address | Email | Comments| +---------+---------+---------+---------+ | ABC | ABC | ABC | Y | | DEF | DEF | DEF | Z | +---------+---------+---------+---------+ 替换为INNER JOIN

Demo Here

答案 1 :(得分:0)

您可以使用joins,如下所示

 select Table_1.*
    from Table_1
    left join Table_2  on 
    (Table_1.id=Table_2.Table_1_id)
    and 
    Table_2.id>
    (select id from Table_2  
        where Table_1_id='10'
        order by id DESC LIMIT 2,1)

答案 2 :(得分:0)

您可以使用MySql JOIN执行此操作,以使用相同的查询从多个表中检索数据。 我看到你使用的是php PDO,你可以像这样使用join:

$stmt = $conn->prepare(" SELECT Table1.*, Table_2.Comment FROM Table_2 
    LEFT JOIN Table_1 ON ( Table_2.Table_1_id=Table_1.id)
    WHERE id = (SELECT id 
    FROM Table_2 WHERE post_id = 10 ORDER BY id DESC LIMIT 1)");

if ($stmt->execute(array($getVars['Comment']))) {
    while ($row = $stmt->fetch()) {
        print_r($row);
    }
}