2016-06-09 36 views
2

我正在構建Laravel拍賣應用程序。我有一個很多的表格和一個出價表格。用於批次和出價的SQL查詢:查找用戶是否出價最高

大量表:

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; 

回答

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 
+1

這件作品非常棒!非常感謝。我做了一些較小的修改,並編輯了上面的問題以包含小修正的版本 – wiwa1978