我正在建立一个Laravel拍卖应用程序。我有一张桌子和一张投标表。
很多桌子:
mysql> select id, lot_name, created_at, updated_at from lots;
+----+----------+---------------------+---------------------+
| id | lot_name | created_at | updated_at |
+----+----------+---------------------+---------------------+
| 1 | Lot 1 | 2016-06-09 14:23:58 | 2016-06-09 14:23:58 |
| 2 | Lot 2 | 2016-06-09 14:23:58 | 2016-06-09 14:23:58 |
| 3 | Lot 3 | 2016-06-09 14:23:58 | 2016-06-09 14:23:58 |
| 4 | Lot 4 | 2016-06-09 14:23:58 | 2016-06-09 14:23:58 |
| 5 | Lot 5 | 2016-06-09 14:23:58 | 2016-06-09 14:23:58 |
| 6 | Lot 6 | 2016-06-09 14:23:58 | 2016-06-09 14:23:58 |
| 7 | Lot 7 | 2016-06-09 14:23:58 | 2016-06-09 14:23:58 |
| 8 | Lot 8 | 2016-06-09 14:23:58 | 2016-06-09 14:23:58 |
| 9 | Lot 9 | 2016-06-09 14:23:58 | 2016-06-09 14:23:58 |
| 10 | Lot 10 | 2016-06-09 14:23:58 | 2016-06-09 14:23:58 |
| 11 | Lot 11 | 2016-06-09 14:23:58 | 2016-06-09 14:23:58 |
| 12 | Lot 12 | 2016-06-09 14:23:58 | 2016-06-09 14:23:58 |
| 13 | Lot 13 | 2016-06-09 14:23:58 | 2016-06-09 14:23:58 |
| 14 | Lot 14 | 2016-06-09 14:23:58 | 2016-06-09 14:23:58 |
| 15 | Lot 15 | 2016-06-09 14:23:58 | 2016-06-09 14:23:58 |
| 16 | Lot 16 | 2016-06-09 14:23:58 | 2016-06-09 14:23:58 |
+----+----------+---------------------+---------------------+
和出价表:
mysql> select * from bids;
+----+--------+---------+-----------+---------------------+---------------------+
| id | lot_id | user_id | bid_value | created_at | updated_at |
+----+--------+---------+-----------+---------------------+---------------------+
| 1 | 1 | 2 | 100 | 2016-06-01 11:14:15 | 2016-06-09 14:23:59 |
| 2 | 1 | 2 | 150 | 2016-06-01 13:19:02 | 2016-06-09 14:23:59 |
| 3 | 1 | 3 | 160 | 2016-06-02 09:02:35 | 2016-06-09 14:23:59 |
| 4 | 1 | 2 | 180 | 2016-06-02 12:25:16 | 2016-06-09 14:23:59 |
| 5 | 2 | 2 | 100 | 2016-06-01 09:26:17 | 2016-06-09 14:23:59 |
| 6 | 2 | 3 | 150 | 2016-06-01 10:14:15 | 2016-06-09 14:23:59 |
| 7 | 2 | 2 | 160 | 2016-06-01 11:35:09 | 2016-06-09 14:23:59 |
| 8 | 3 | 3 | 180 | 2016-06-04 11:14:15 | 2016-06-09 14:23:59 |
| 9 | 1 | 3 | 200 | 2016-06-03 19:29:56 | 2016-06-09 14:23:59 |
| 10 | 3 | 2 | 250 | 2016-06-04 23:14:15 | 2016-06-09 14:23:59 |
| 11 | 4 | 2 | 500 | 2016-06-03 09:16:39 | 2016-06-09 14:23:59 |
| 12 | 1 | 3 | 230 | 2016-06-01 11:14:15 | 2016-06-09 14:23:59 |
| 13 | 1 | 2 | 250 | 2016-06-01 11:14:15 | 2016-06-09 14:23:59 |
| 14 | 3 | 2 | 300 | 2016-06-05 02:14:15 | 2016-06-09 14:23:59 |
| 15 | 2 | 3 | 180 | 2016-06-01 20:44:15 | 2016-06-09 14:23:59 |
| 16 | 4 | 2 | 600 | 2016-06-04 08:59:15 | 2016-06-09 14:23:59 |
| 17 | 1 | 3 | 300 | 2016-06-03 17:20:30 | 2016-06-09 14:23:59 |
| 18 | 1 | 3 | 350 | 2016-06-04 18:04:55 | 2016-06-09 14:23:59 |
| 19 | 7 | 2 | 160 | 2016-06-02 22:24:02 | 2016-06-09 14:23:59 |
| 20 | 1 | 3 | 400 | 2016-06-05 07:00:27 | 2016-06-09 14:23:59 |
| 21 | 8 | 3 | 100 | 2016-06-02 11:58:29 | 2016-06-09 14:23:59 |
| 22 | 1 | 3 | 450 | 2016-06-05 10:09:06 | 2016-06-09 14:23:59 |
| 23 | 1 | 2 | 480 | 2016-06-05 14:12:15 | 2016-06-09 14:23:59 |
| 24 | 5 | 2 | 1000 | 2016-06-02 13:38:34 | 2016-06-09 14:23:59 |
| 25 | 1 | 2 | 525 | 2016-06-05 20:39:29 | 2016-06-09 14:23:59 |
| 26 | 1 | 3 | 600 | 2016-06-05 23:59:50 | 2016-06-09 14:23:59 |
| 27 | 6 | 2 | 1000 | 2016-06-01 07:00:02 | 2016-06-09 14:23:59 |
| 28 | 6 | 3 | 1200 | 2016-06-04 23:14:56 | 2016-06-09 14:23:59 |
+----+--------+---------+-----------+---------------------+---------------------+
关系批次 - 出价:一批可以包含许多出价(来自许多用户),每个出价只属于一个批次和一个用户。
我的目标是创建一个视图,显示用户是否具有给定批次的最高出价。我将在绿色/红色行颜色的表格中指明(如果用户对该批次的出价最高,则为绿色)。
理想情况下,我想实现以下目标(因此我可以创建一个Laravel查询并将其发送到视图以循环遍历数组)
+--------+---------+----------------+----------------+
| lot_id | user_id | max(bid_value) | highest value | extra columns from the lots table such as created_at, updated_at
+--------+---------+----------------+----------------+
| 1 | 2 | 525 | 600 | ....
| 2 | 2 | 160 | 180 | ....
| 3 | 2 | 300 | 300 | ....
| 4 | 2 | 600 | 600 | ....
| 5 | 2 | 1000 | 1000 | ....
| 6 | 2 | 1000 | 1200 | ....
| 7 | 2 | 160 | 160 | ....
+--------+--------------------------+----------------+
我接触到以下内容:
mysql> select lot_id, max(bid_value) as 'highest value' from bids group by lot_id;
+--------+---------------+
| lot_id | highest value |
+--------+---------------+
| 1 | 600 |
| 2 | 180 |
| 3 | 300 |
| 4 | 600 |
| 5 | 1000 |
| 6 | 1200 |
| 7 | 160 |
| 8 | 100 |
+--------+---------------+
和:
mysql> select lot_id, max(bid_value) as user_highest_bid from bids where user_id=2 group by lot_id;
+--------+------------------+
| lot_id | user_highest_bid |
+--------+------------------+
| 1 | 525 |
| 2 | 160 |
| 3 | 300 |
| 4 | 600 |
| 5 | 1000 |
| 6 | 1000 |
| 7 | 160 |
+--------+------------------+
我如何将其结合起来以实现我想要的表格,包括其他批次信息以及用户已经出价的这些批次?
----编辑---- 从Mureinik添加正确的答案(稍作修改):
mysql> SELECT lots.*, b.*
-> FROM lots
-> JOIN (SELECT lot_id,
-> 2 AS user_id,
-> MAX(bid_value) AS highest_value,
-> MAX(CASE user_id WHEN 2 THEN bid_value END) AS highest_bid
-> FROM bids
-> GROUP BY lot_id) b ON lots.id = b.lot_id AND highest_bid IS NOT NULL;
答案 0 :(得分:1)
您需要两个查询 - 一个用于批次的详细信息,另一个用于汇总出价。这里一个巧妙的技巧是对特定用户的最高出价使用case语句,因此您只需要一个聚合查询。
在此查询中,我假设您只想要用户实际出价的结果。如果不是这样,只需将not null
条件放在连接中:
SELECT lot.*, b.*
FROM lots
JOIN (SELECT lot_id,
2 AS user_id,
MAX(bid_value) AS highest_value,
MAX(CASE user_id WHEN 2 THEN bid_value END) AS highest_bid
FROM bids
GROUP BY lot_id) b ON lot.lot_id = b.lot_id AND highest_bid IS NOT NULL