我有一个订单表(orders),一个状态表(order\u status)和一个pivot表,它是一个状态日志(order\u order\u status)。
当订单的状态更改时,将向透视表中添加一个条目。透视表中该订单的最新条目将是其当前状态。
我需要能够显示所有订单,目前有一个给定的状态。例如,所有处于“报价”状态的订单。有没有一个雄辩的方法来构建这个查询?
(编辑,补充说明:订单的当前状态是状态日志中带有最新“创建时间”日期的条目。)
以下是一些模式示例:
mysql> SELECT * FROM orders WHERE id = 2;
+----+---------+--------------+---------------+----------------------+---------------------+---------------------+
| id | user_id | order_number | job_reference | accounting_reference | created_at | updated_at |
+----+---------+--------------+---------------+----------------------+---------------------+---------------------+
| 2 | 73 | 37-5 | Janis Joplin | NULL | 2018-06-25 02:27:21 | 2018-06-25 02:27:21 |
+----+---------+--------------+---------------+----------------------+---------------------+---------------------+
mysql> SELECT * FROM order_order_status WHERE order_id = 2 ORDER BY created_at;
+------+----------+-----------------+---------+---------------------+---------------------+
| id | order_id | order_status_id | user_id | created_at | updated_at |
+------+----------+-----------------+---------+---------------------+---------------------+
| 2 | 2 | 2 | 753 | 2012-06-27 09:47:00 | 2012-06-27 09:47:00 |
| 3 | 2 | 3 | 753 | 2012-06-27 09:56:00 | 2012-06-27 09:56:00 |
| 4 | 2 | 4 | 753 | 2012-06-27 09:56:00 | 2012-06-27 09:56:00 |
| 5 | 2 | 5 | 1153 | 2012-06-27 10:13:00 | 2012-06-27 10:13:00 |
| 6 | 2 | 6 | 1153 | 2012-06-27 10:13:00 | 2012-06-27 10:13:00 |
| 7 | 2 | 10 | 1153 | 2012-06-27 10:13:00 | 2012-06-27 10:13:00 |
| 8 | 2 | 7 | 1153 | 2012-06-27 10:13:00 | 2012-06-27 10:13:00 |
| 9 | 2 | 10 | 1153 | 2012-06-27 10:42:00 | 2012-06-27 10:42:00 |
| 10 | 2 | 7 | 1153 | 2012-06-27 10:42:00 | 2012-06-27 10:42:00 |
| 11 | 2 | 8 | 753 | 2012-06-27 10:44:00 | 2012-06-27 10:44:00 |
| 12 | 2 | 9 | 753 | 2012-06-27 10:45:00 | 2012-06-27 10:45:00 |
| 2222 | 2 | 10 | 54 | 2013-01-03 12:08:00 | 2013-01-03 12:08:00 |
+------+----------+-----------------+---------+---------------------+---------------------+
mysql> SELECT * FROM order_statuses;
+----+----------------+----------------+---------------------+---------------------+
| id | title | tag | created_at | updated_at |
+----+----------------+----------------+---------------------+---------------------+
| 1 | Archived Quote | archived_quote | 2018-06-25 02:25:28 | 2018-06-25 02:25:28 |
| 2 | Quote | quote | 2018-06-25 02:25:28 | 2018-06-25 02:25:28 |
| 3 | Order | order | 2018-06-25 02:25:28 | 2018-06-25 02:25:28 |
| 4 | Confirmed | confirmed | 2018-06-25 02:25:28 | 2018-06-25 02:25:28 |
| 5 | Manufacturing | manufacturing | 2018-06-25 02:25:28 | 2018-06-25 02:25:28 |
| 6 | Painting | painting | 2018-06-25 02:25:28 | 2018-06-25 02:25:28 |
| 7 | Dispatched | dispatched | 2018-06-25 02:25:28 | 2018-06-25 02:25:28 |
| 8 | Invoiced | invoiced | 2018-06-25 02:25:28 | 2018-06-25 02:25:28 |
| 9 | Paid | paid | 2018-06-25 02:25:28 | 2018-06-25 02:25:28 |
| 10 | Closed | closed | 2018-06-25 02:25:28 | 2018-06-25 02:25:28 |
| 11 | Archived | archived | 2018-06-25 02:25:28 | 2018-06-25 02:25:28 |
+----+----------------+----------------+---------------------+---------------------+
编辑:进一步澄清。这是返回所需结果的sql查询。我正在寻找一种雄辩的方法来获得同样的结果:
SELECT a.order_status_id, c.*
FROM order_order_status a
INNER JOIN (
SELECT order_id, MAX(updated_at) last_date
FROM order_order_status
GROUP BY order_id
) b ON a.order_id = b.order_id AND a.updated_at = b.last_date
INNER JOIN
orders c
ON c.id = a.order_id
WHERE a.order_status_id = (SELECT id from order_statuses where tag="closed")
3条答案
按热度按时间mznpcxlj1#
你必须使用连接。在获取多对多关系时,eloquent会创建两个查询。不幸的是,在这种情况下不能使用order by。
更新
查询生成器将是您的最佳选择,不幸的是,被接受的答案将是非常低效的,至少不能很好地扩展。在我脑子里,这个问题大概是这样的:
gv8xihay2#
php工匠make:model user
在您的用户模型中:
php工匠make:model order
在您的订单型号中:
php工匠make:model orderstatus
在orderstatus模型中:
php工匠make:controller orderscontroller
现在您想通过orderstatus title='quote'获取订单
在orderscontroller中:
m4pnthwp3#
您需要在每个模型中添加一些关系才能将它们连接起来
订单
订单\订单\状态
订单状态
然后你就会这样做
应该买这样的东西
注意,我在表名前面加了
t_
但是你可以把它们命名为和你的模型一样的名字。更新
为此,您可以使用这样的方法传递一个clusre,该clusre将根据状态id进行过滤
将给予
它将返回所有订单,但您可以使用
status_log
弄清楚它在t_order_order_status
表中,但如果您想删除“额外”并仅保留记录器表中存在的那些订单,则可以在$result
.