我想知道使用PHP进行以下SQL调用的最快方法是什么。我正在使用程序化的mySQLi。
$dcount = "SELECT max(id) FROM deposits";
$result = mysqli_query($conn,$dcount);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$count = $row["max(id)"];
echo $count;
echo ",";
}
} else {
echo '0';
}
//second query
$ucount = "SELECT max(int) FROM anothertable";
$result2 = mysqli_query($conn,$ucount);
if (mysqli_num_rows($result2) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$count = $row["max(int)"];
echo $count;
echo ",";
}
} else {
echo '0';
}
有没有办法让执行速度比这样快,可能是在第一个if语句之后回显两个查询的结果?
对我而言似乎相当长。
提前致谢。
答案 0 :(得分:2)
SELECT max(id) as max_id, (SELECT max(int) as max_int FROM anothertable) as max_int
FROM deposits
未经测试,但类似的应该可以使用
答案 1 :(得分:1)
SELECT d.max(id) as d_max_id, a.max(int) as a_max_int FROM deposits as d JOIN anothertable as a ON d.id = a.id;
是您需要的多个表
$row['d_max_id']
现在会给你deposits.max(id)
您必须根据您希望两个表格匹配的内容编辑d.id = a.id
如果你不能加入试试这个:
SELECT max(id) as max_id, (SELECT max(int) FROM anothertable) as max_int FROM deposits;
答案 2 :(得分:1)
SELECT max(id) as max_id, max(int) as max_int FROM deposits ,anothertable
答案 3 :(得分:1)
$dcount = "
SELECT max(id) as max,'deposit' as table_name FROM deposits
UNION
SELECT max(id) as max,'another' as table_name FROM anothertable
";
$result = mysqli_query($conn,$dcount);
if (mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
echo $row["table_name"].":".$row["max"]."\n";
}
} else {
echo '0';
}