MYSQL嵌套查询

时间:2013-10-11 16:23:05

标签: php mysql

提出这个问题的目的只是学习如何在MYSQL中进行嵌套查询。

1)以下查询有什么问题?

"SELECT tblwriter.writer_alias, tblwriter.writer_first_name, COUNT(tblordersub.suborder_alias) AS totalOrders FROM tblwriter, tblordersub WHERE tblwriter.writer_isactive = 1 AND tblordersub.writer_alias = tblwriter.writer_alias"

2)可以为以下MYSQL查询嵌套提供更好的解决方案吗?

$currentorders = $db->rawQuery("SELECT order_title,order_alias FROM tblorder WHERE company_id=? AND YEAR(order_date) = YEAR(CURDATE()) AND MONTH(order_date) = MONTH(CURDATE())",$params);
$orderssummary = array();
if(!empty($currentorders)){
    foreach($currentorders as $corder){
        $param = array($corder["order_alias"]);
        $oprice = $db->rawQuery("SELECT payment_amount FROM tblpayment WHERE payment_status = 1 AND writer_alias IS NULL AND order_alias=?",$param);
        $itssuborders = $db->rawQuery("SELECT suborder_alias FROM tblordersub WHERE order_alias=?",$param);
        $thesuborders = array_implode("",",",$itssuborders);
        $cost = $db->rawQuery("SELECT SUM(payment_amount) AS total_subtotal FROM tblpayment WHERE writer_alias IS NOT NULL AND suborder_alias IN (".$thesuborders.")");
        $orderssummary[] = array("title"=>$corder["order_title"],"price"=>$oprice[0]["payment_amount"],"cost"=>$cost[0]["total_subtotal"]);
    }
}

3)有没有办法将以下三个查询合并为1?

"SELECT SUM(payment_amount) AS totalAmount FROM tblpayment WHERE company_id=? AND payment_status = 1 AND order_alias IS NOT NULL AND YEAR(payment_add_datetime) = YEAR(CURDATE()) AND MONTH(payment_add_datetime) = MONTH(CURDATE() - INTERVAL 2 MONTH)"
"SELECT SUM(payment_amount) AS totalAmount FROM tblpayment WHERE company_id=? AND payment_status = 0 AND writer_alias IS NOT NULL AND YEAR(payment_add_datetime) = YEAR(CURDATE()) AND MONTH(payment_add_datetime) = MONTH(CURDATE() - INTERVAL 2 MONTH)"
"SELECT SUM(payment_amount) AS totalAmount FROM tblpayment WHERE company_id=? AND payment_status = 1 AND writer_alias IS NOT NULL AND YEAR(payment_add_datetime) = YEAR(CURDATE()) AND MONTH(payment_add_datetime) = MONTH(CURDATE() - INTERVAL 2 MONTH)"

4)与第二个问题相同,有没有办法将以下查询合并到一个查询中?

$biggest_customers = $db->rawQuery("SELECT payment_user_id, SUM(payment_amount) AS totalEARNED FROM tblpayment WHERE writer_alias IS NULL AND order_alias IS NOT NULL GROUP BY payment_user_id ORDER BY totalEARNED DESC LIMIT 10");
for($i=0;$i<count($biggest_customers);$i++){
    $params = array($biggest_customers[$i]["payment_user_id"]);
    $customerinformation = $db->rawQuery('SELECT customer_alias FROM tblcustomer WHERE user_id=?',$params);
    $biggest_customers[$i]["customer_alias"] = $customerinformation[0]["customer_alias"];
    unset($biggest_customers[$i]["payment_user_id"]);
}

希望学习一些事情!!

2 个答案:

答案 0 :(得分:2)

  1. 查看汇总查询
  2. 参见JOINs
  3. 请参阅CASE语句/ PIVOT TABLE查询
  4. 再次参阅JOIN

答案 1 :(得分:0)

在选择多个列时,如果没有GROUP BY,则无法使用COUNT()语句(聚合查询)。您还应该使用join语句从不同的表中选择值。这样的事情可能是:

SELECT tblwriter.writer_alias, tblwriter.writer_first_name, COUNT(tblordersub.suborder_alias) AS totalOrders 
FROM tblwriter 
INNER JOIN tblordersub ON (tblordersub.writer_alias = tblwriter.writer_alias) 
WHERE tblwriter.writer_isactive = 1
GROUP BY tblordersub.suborder_alias

只需在查询中使用FROM tblwriter, tblordersub即可创建交叉联接,即表中数据的cartesian product。你可能不希望这样。