How I can merge this 2 querys to 1?
$query1 = mysqli_query($_connect, 'SELECT `id` FROM `callers` ORDER BY `id` DESC LIMIT 1') or die();
$result1 = mysqli_fetch_object($query1);
$query2 = mysqli_query($_connect, 'SELECT SUM(`calls`) AS "total" FROM `callers`') or die();
$result2 = mysqli_fetch_object($query2);
Now I have:
$result1->id
for the last ID
and
$result2->total
for a Sum of all calls
For that results I have 2 querys, my queation: Is it possible to run 1 query and have both result?
2
Answers
You can join the two queries.
You can then use
$result->id
and$result->total
.Well, I’ve got to take back what I wrote before, because I think there is a more optimal way to combine these two queries:
Note that using
MIN()
gets the single lowest id, just like your first query does.However, unless you runs these queries tens or hundreds of time per second, you won’t notice a big improvement.