MySQL 通过JOIN查询多张表数据的总和

标签:2023-11-30 23:36:30
<?php
// 连接数据库
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
 
$conn = new mysqli($servername, $username, $password, $dbname);
 
// 检查连接是否成功
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
 
// 查询多表的总数
$sql = "SELECT COUNT(*) AS total_count FROM table1
        JOIN table2 ON table1.id = table2.id
        JOIN table3 ON table1.id = table3.id";
 
$result = $conn->query($sql);
 
if ($result->num_rows > 0) {
    // 输出总数
    while($row = $result->fetch_assoc()) {
        echo "总数: " . $row["total_count"];
    }
} else {
    echo "没有结果";
}
 
// 关闭数据库连接
$conn->close();
?>
原文出处:http://www.dongblog.com/notes/47.html
来源:博客网 转载请注明出处!

活跃用户

日月星辰
Ta还没有签名
A-ONE
Ta还没有签名
潮牌连锁
Ta还没有签名
会持续四季
Ta还没有签名

友情链接


Warning: Smarty error: unable to read resource: "../../../templates/default/./common/foot/footer_index.htm" in /usr/home/hyu3925200001/htdocs/common/smarty/Smarty.class.php on line 1093

Warning: Smarty error: unable to read resource: "../../../templates/default/./common/foot/footer_index.htm" in /usr/home/hyu3925200001/htdocs/common/smarty/Smarty.class.php on line 1093