假设我想在3个不同的表上执行3个不同的总和
2017-01-29 03:28:09 GMT
我想在一个声明中执行这些查询。
SELECT sum(`diepoints`) as exp FROM `dies` where user='coder'
SELECT sum(`winpoints`) as rev FROM `wins` where user='coder'
SELECT sum(`debtpoints`) as deb FROM `debts` where user='coder'
然后把它们放在下面。
$resource1= $con->query(SELECT sum(`diepoints`) as exp FROM `dies` where user='coder');
$resource2 = $con->query(SELECT sum(`winpoints`) as rev FROM `wins` where user='coder');
$resource3 = $con->query(SELECT sum(`debtpoints`) as deb FROM `debts` where user='coder');
关于如何组合这些查询的任何帮助说明都会很棒。
答案 0 :(得分:0)
一种简单的方法是子查询:
SELECT (SELECT sum(`diepoints`) as exp FROM `dies` where user='coder') as exp,
(SELECT sum(`winpoints`) as rev FROM `wins` where user='coder') as rev,
(SELECT sum(`debtpoints`) as deb FROM `debts` where user='coder') as deb
答案 1 :(得分:0)
使用如下子查询在一行中选择它们:
select
(SELECT sum(`diepoints`) as exp FROM `dies` where user='coder') exp,
(SELECT sum(`winpoints`) as rev FROM `wins` where user='coder') rev,
(SELECT sum(`debtpoints`) as deb FROM `debts` where user='coder') deb