使用内部联接按位置对数据进行分组

时间:2013-04-17 08:31:28

标签: mysql pdf laravel

您好我想找到一种方法将所有订单结果分组到不同的位置,因为我想将每个位置的订单单输出到PDF中,如下所示:

location1:new york:address1,address2,state,zip code

order1 - 员工姓名

order2 - 员工姓名

order3 - 员工姓名

location2:alabama:address1,address2,州,邮政编码

order1 - 员工姓名

order2 - 员工姓名

order3 - 员工姓名

我使用MySQL

完成了这个查询
SELECT locations.loc_id, locations.address1, locations.address2, locations.state, 
locations.zip_code, orders.order_id, staff.staff_id, staff.first_name, 
staff.last_name, orders.created_at, items.item_id, items.name
FROM locations
INNER JOIN location_staff
ON locations.loc_id = location_staff.loc_id
INNER JOIN staff
ON location_staff.staff_id = staff.staff_id
INNER JOIN orders
ON staff.staff_id = orders.staff_id
INNER JOIN items
ON orders.item_id = items.item_id
GROUP BY locations.loc_id;

这会使结果恢复正常。这是ERD以便更清楚地理解。

https://www.dropbox.com/s/7inma4s42xq5t4a/ERD.jpg

我正在尝试使用Laravel框架和html2pdf库将其输出为PDF:

    public function get_report() {

    $orders = DB::query(
        'SELECT orders.order_id, staff.staff_id, staff.first_name, 
            staff.last_name, items.item_id, items.name, 
            orders.created_at, locations.address1, locations.address2, locations.state, 
            locations.zip_code
        FROM locations
        INNER JOIN staff
        ON orders.staff_id = staff.staff_id
        INNER JOIN items
        ON orders.item_id = items.item_id
        INNER JOIN location_staff
        ON location_staff.staff_id = staff.staff_id
        INNER JOIN locations
        ON location_staff.loc_id = locations.loc_id;'
    );


    try {
        $html2pdf = new HTML2PDF('P','A4','en');
        $html2pdf->pdf->SetDisplayMode('fullpage');
        $html2pdf->WriteHTML(View::make('home.report', $orders));
        $html2pdf->Output('orders.pdf');
        exit;
        }
    catch(HTML2PDF_exception $e) {
        echo $e;
        exit;
    }

    return View::make('home.report');
}

这是正确的查询,以这样的方式输出分组结果:

foreach($orders as $order){

    echo(
        $order->order_id,
        $order->staff_id,
        $order->first_name,
        $order->last_name,
        $order->item_id,
        $order->name,
        $order->created_at,
        $order->address1,
        $order->address2,
        $order->state,
        $order->zip_code
        );
    }

0 个答案:

没有答案