博主:DongJiang
码龄:7年
等级:LV.22级
内容:316
今日访问:2312
访问总量:5936
博客简介:学习与分享
博客创建时间:2018-04-12
博客主页 立即前往
赞助位
成为赞助商

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();
?>
原文出处:
版权声明:本文来源地址若非本站均为转载,若侵害到您的权利,请及时联系我们,我们会在第一时间进行处理。