The results associated with the query will laravel many deduplication processing

To explain the structure of the table

  • The main table (Orders table) order data
ord_id order_sn
1 EX2019100123458

Wherein order_id primary key (order id)

  • Child table (table tickets) order_item data
ord_ite_id ord_id exhibit_sn type
1 1 EXSN20191001001 1
2 1 EXSN20191001002 1
3 1 EXSN20191001003 1

Between the main table and the child tables by associating ord_id

Implementation: by type (type of ticket) by the child table query order number in the primary table

  The first attempt:

select DISTINCT `order`.ord_id from `order` INNER JOIN order_item on order.ord_id = order_item.ord_id where type = 1;

This method can be achieved, but if you want to page, then use laravel in need so

DB::table(DB::raw("sql语句"))->paginate(15);

But then our native sql statement is equivalent to use, but if you add conditions, then it can only go splicing sql statement

  The second attempt:

select `order`.ord_id,`order`.order_sn from `order` INNER JOIN (select distinct ord_id from order_item) r on `order`.ord_id=r.ord_id

In use in larave

Order::join(DB::raw("(select distinct order_item.ord_id item_ord_id,type from order_item) ".env("DB_PREFIX")."bb"),"bb.item_order_id","exhibit_order.order_id")->paginate(15);

So that we can continue to be used where () method to add conditions

Guess you like

Origin www.cnblogs.com/itsuibi/p/11617512.html