Php 教育-组合SQL查询(联合所有示例)
我试图在编程时更好地使用SQL,并希望将以下3个SQL查询/代码组合到一个SQL调用中-可能吗?(我意识到它不会100%格式化,因为我有明确的Php 教育-组合SQL查询(联合所有示例),php,sql,Php,Sql,我试图在编程时更好地使用SQL,并希望将以下3个SQL查询/代码组合到一个SQL调用中-可能吗?(我意识到它不会100%格式化,因为我有明确的每月\u总数,但如果可能的话…?) 注意:下面提供的代码应该让您了解我的数据库的结构以及我试图实现的目标 电流输出 当然可以。我添加了一个QueryDisc列来区分查询 $ret = array( 'raw' => array(), 'total' => '0.00', 'pending' => '0.00',
每月\u总数
,但如果可能的话…?)
注意:下面提供的代码应该让您了解我的数据库的结构以及我试图实现的目标
电流输出
当然可以。我添加了一个QueryDisc列来区分查询
$ret = array(
'raw' => array(),
'total' => '0.00',
'pending' => '0.00',
'monthly_totals' => array()
);
if (!empty($sales_rep_id)) {
$raw_query_data = $this->query("
SELECT
'detail' query_desc,
id,
invoice_no,
CONCAT(firstname, ' ', lastname) as name,
affiliate_id,
commission,
YEAR(created) as created_year,
MONTH(created) as created_month,
created
FROM orders
WHERE
active = 1 AND
affiliate_id = {$sales_rep_id}
UNION ALL
SELECT
'total' query_desc,
NULL id,
NULL invoice_no,
NULL name,
NULL affiliate_id,
SUM(commission) as commission_total,
NULL created_year,
NULL created_month,
NULL created
FROM orders
WHERE
active = 1 AND
affiliate_id = {$sales_rep_id}
UNION ALL
SELECT
'monthly_total' query_desc,
NULL id,
NULL invoice_no,
NULL name,
NULL affiliate_id,
SUM(commission) as commission_total,
NULL created_year,
MONTH(created) as created_month,
NULL created
FROM orders
WHERE
active = 1 AND
affiliate_id = {$sales_rep_id}
GROUP BY MONTH(created)
ORDER BY created_month DESC
");
foreach ($raw_query_data as $data) {
switch ($data['query_desc']) {
case 'detail':
unset($data['query_desc']);
$ret['raw'][] = $data;
break;
case 'monthly_total':
$ret['monthly_totals'][$data['created_month']] = $data['commission'];
break;
case 'total':
$ret['total'] = $data['commission'];
break;
}
}
}
return $ret;
输出[参考]
一般来说:如果您运行的是嵌套查询,而内部查询依赖于/使用来自外部查询的数据,那么通常您可以将它们组合成一个
联接ed查询。对,但我不知道如何:(1)按分组,并获得带有每月总计的单个“原始”订单数据以及(2)我从来没有见过那种语法'total'QueryDesc
或NULL id
的NULL部分,不过我是自学成才的。有什么资源可以让我解释一下它的作用吗?我的意思是:我知道它的作用-设置默认值-但我想了解更多:DI可以解释它的作用:)'Total'QueryDesc
向select语句添加一列,其中包含字符串值'Total'
NULL id
为id
列传递一个NULL
,因为后两个查询是聚合的,不需要该列。@Wallter您可能见过这样的代码:从someTable中选择someColumn作为someAlias
。AS
是可选的(但包含在其中不是一个坏主意)。在本例中,使用字符串和空值代替列名。发布的示例相当于选择'total'作为查询描述,NULL作为id,
。
array(4) {
["raw"]=>
array(5) {
[0]=>
array(8) {
["id"]=>
string(4) "5335"
["invoice_no"]=>
string(15) "5335-1395260183"
["name"]=>
string(10) "First Last"
["affiliate_id"]=>
string(1) "6"
["commission"]=>
string(6) "100.88"
["created_year"]=>
string(4) "2014"
["created_month"]=>
string(1) "3"
["created"]=>
string(19) "2014-03-19 14:16:23"
}
[1]=>
array(8) {
["id"]=>
string(4) "5373"
["invoice_no"]=>
string(15) "5373-1396031594"
["name"]=>
string(10) "First Last"
["affiliate_id"]=>
string(1) "6"
["commission"]=>
string(6) "294.27"
["created_year"]=>
string(4) "2014"
["created_month"]=>
string(1) "3"
["created"]=>
string(19) "2014-03-28 12:33:14"
}
[2]=>
array(8) {
["id"]=>
string(4) "5374"
["invoice_no"]=>
string(15) "5374-1396043777"
["name"]=>
string(17) "First M. Last"
["affiliate_id"]=>
string(1) "6"
["commission"]=>
string(6) "122.16"
["created_year"]=>
string(4) "2014"
["created_month"]=>
string(1) "3"
["created"]=>
string(19) "2014-03-28 15:56:17"
}
[3]=>
array(8) {
["id"]=>
string(4) "5378"
["invoice_no"]=>
string(15) "5378-1396044619"
["name"]=>
string(10) "First Last"
["affiliate_id"]=>
string(1) "6"
["commission"]=>
string(6) "100.88"
["created_year"]=>
string(4) "2014"
["created_month"]=>
string(1) "3"
["created"]=>
string(19) "2014-03-28 16:10:19"
}
[4]=>
array(8) {
["id"]=>
string(4) "5372"
["invoice_no"]=>
string(15) "5372-1396031586"
["name"]=>
string(10) "First Last"
["affiliate_id"]=>
string(1) "6"
["commission"]=>
string(6) "294.27"
["created_year"]=>
string(4) "2014"
["created_month"]=>
string(1) "2"
["created"]=>
string(19) "2014-02-28 12:33:06"
}
}
["total"]=>
string(6) "912.46"
["pending"]=>
string(4) "0.00"
["monthly_totals"]=>
array(2) {
[2]=>
string(6) "294.27"
[3]=>
string(6) "618.19"
}
}
$ret = array(
'raw' => array(),
'total' => '0.00',
'pending' => '0.00',
'monthly_totals' => array()
);
if (!empty($sales_rep_id)) {
$raw_query_data = $this->query("
SELECT
'detail' query_desc,
id,
invoice_no,
CONCAT(firstname, ' ', lastname) as name,
affiliate_id,
commission,
YEAR(created) as created_year,
MONTH(created) as created_month,
created
FROM orders
WHERE
active = 1 AND
affiliate_id = {$sales_rep_id}
UNION ALL
SELECT
'total' query_desc,
NULL id,
NULL invoice_no,
NULL name,
NULL affiliate_id,
SUM(commission) as commission_total,
NULL created_year,
NULL created_month,
NULL created
FROM orders
WHERE
active = 1 AND
affiliate_id = {$sales_rep_id}
UNION ALL
SELECT
'monthly_total' query_desc,
NULL id,
NULL invoice_no,
NULL name,
NULL affiliate_id,
SUM(commission) as commission_total,
NULL created_year,
MONTH(created) as created_month,
NULL created
FROM orders
WHERE
active = 1 AND
affiliate_id = {$sales_rep_id}
GROUP BY MONTH(created)
ORDER BY created_month DESC
");
foreach ($raw_query_data as $data) {
switch ($data['query_desc']) {
case 'detail':
unset($data['query_desc']);
$ret['raw'][] = $data;
break;
case 'monthly_total':
$ret['monthly_totals'][$data['created_month']] = $data['commission'];
break;
case 'total':
$ret['total'] = $data['commission'];
break;
}
}
}
return $ret;
array(8) {
[0]=>
array(9) {
["query_desc"]=>
string(6) "detail"
["id"]=>
string(4) "5335"
["invoice_no"]=>
string(15) "5335-1395260183"
["name"]=>
string(10) "First Last"
["affiliate_id"]=>
string(1) "6"
["commission"]=>
string(6) "100.88"
["created_year"]=>
string(4) "2014"
["created_month"]=>
string(1) "3"
["created"]=>
string(19) "2014-03-19 14:16:23"
}
[1]=>
array(9) {
["query_desc"]=>
string(13) "monthly_total"
["id"]=>
NULL
["invoice_no"]=>
NULL
["name"]=>
NULL
["affiliate_id"]=>
NULL
["commission"]=>
string(6) "618.19"
["created_year"]=>
NULL
["created_month"]=>
string(1) "3"
["created"]=>
NULL
}
[2]=>
array(9) {
["query_desc"]=>
string(6) "detail"
["id"]=>
string(4) "5378"
["invoice_no"]=>
string(15) "5378-1396044619"
["name"]=>
string(10) "First Last"
["affiliate_id"]=>
string(1) "6"
["commission"]=>
string(6) "100.88"
["created_year"]=>
string(4) "2014"
["created_month"]=>
string(1) "3"
["created"]=>
string(19) "2014-03-28 16:10:19"
}
[3]=>
array(9) {
["query_desc"]=>
string(6) "detail"
["id"]=>
string(4) "5374"
["invoice_no"]=>
string(15) "5374-1396043777"
["name"]=>
string(17) "First M. Last"
["affiliate_id"]=>
string(1) "6"
["commission"]=>
string(6) "122.16"
["created_year"]=>
string(4) "2014"
["created_month"]=>
string(1) "3"
["created"]=>
string(19) "2014-03-28 15:56:17"
}
[4]=>
array(9) {
["query_desc"]=>
string(6) "detail"
["id"]=>
string(4) "5373"
["invoice_no"]=>
string(15) "5373-1396031594"
["name"]=>
string(10) "First Last"
["affiliate_id"]=>
string(1) "6"
["commission"]=>
string(6) "294.27"
["created_year"]=>
string(4) "2014"
["created_month"]=>
string(1) "3"
["created"]=>
string(19) "2014-03-28 12:33:14"
}
[5]=>
array(9) {
["query_desc"]=>
string(13) "monthly_total"
["id"]=>
NULL
["invoice_no"]=>
NULL
["name"]=>
NULL
["affiliate_id"]=>
NULL
["commission"]=>
string(6) "294.27"
["created_year"]=>
NULL
["created_month"]=>
string(1) "2"
["created"]=>
NULL
}
[6]=>
array(9) {
["query_desc"]=>
string(6) "detail"
["id"]=>
string(4) "5372"
["invoice_no"]=>
string(15) "5372-1396031586"
["name"]=>
string(10) "First Last"
["affiliate_id"]=>
string(1) "6"
["commission"]=>
string(6) "294.27"
["created_year"]=>
string(4) "2014"
["created_month"]=>
string(1) "2"
["created"]=>
string(19) "2014-02-28 12:33:06"
}
[7]=>
array(9) {
["query_desc"]=>
string(5) "total"
["id"]=>
NULL
["invoice_no"]=>
NULL
["name"]=>
NULL
["affiliate_id"]=>
NULL
["commission"]=>
string(6) "912.46"
["created_year"]=>
NULL
["created_month"]=>
NULL
["created"]=>
NULL
}
}