2016-06-10 32 views
0

我有一個表的拍賣和一張桌子地段:SQL查詢:找到很多屬於目前拍賣

mysql> select id, auction_name, auction_startdate, auction_planned_closedate from auctions; 
+----+--------------+---------------------+---------------------------+ 
| id | auction_name | auction_startdate | auction_planned_closedate | 
+----+--------------+---------------------+---------------------------+ 
| 1 | Auction 1 | 2016-05-01 00:00:00 | 2016-06-30 00:00:00  | 
| 2 | Auction 2 | 2016-06-01 00:00:00 | 2016-07-30 00:00:00  | 
| 3 | Auction 3 | 2016-07-01 00:00:00 | 2016-08-30 00:00:00  | 
| 4 | Auction 4 | 2016-09-01 00:00:00 | 2016-10-30 00:00:00  | 
+----+--------------+---------------------+---------------------------+ 

mysql> select id, auction_id, lot_name from lots; 
+----+------------+----------+ 
| id | auction_id | lot_name | 
+----+------------+----------+ 
| 1 |   1 | Lot 1 | 
| 2 |   1 | Lot 2 | 
| 3 |   1 | Lot 3 | 
| 4 |   1 | Lot 4 | 
| 5 |   1 | Lot 5 | 
| 6 |   1 | Lot 6 | 
| 7 |   1 | Lot 7 | 
| 8 |   2 | Lot 8 | 
| 9 |   2 | Lot 9 | 
| 10 |   2 | Lot 10 | 
| 11 |   3 | Lot 11 | 
| 12 |   3 | Lot 12 | 
| 13 |   3 | Lot 13 | 
| 14 |   3 | Lot 14 | 
| 15 |   4 | Lot 15 | 
| 16 |   4 | Lot 16 | 
+----+------------+----------+ 

我想只顯示當前的拍賣地段(這是拍賣1和2中的示例),換言之,當前時間介於'auction_startdate'和'auction_planned_closedate'之間。

因此,這裏是我想達到的目標:

+--------------+---------------------+---------------------------+---------+ 
| auction_name | auction_startdate | auction_planned_closedate | lots_id | 
+--------------+---------------------+---------------------------+---------+ 
| Auction 1 | 2016-05-01 00:00:00 | 2016-06-30 00:00:00  | 1  | 
| Auction 1 | 2016-05-01 00:00:00 | 2016-06-30 00:00:00  | 2  | 
| Auction 1 | 2016-05-01 00:00:00 | 2016-06-30 00:00:00  | 3  | 
| Auction 1 | 2016-05-01 00:00:00 | 2016-06-30 00:00:00  | 4  | 
| Auction 1 | 2016-05-01 00:00:00 | 2016-06-30 00:00:00  | 5  | 
| Auction 1 | 2016-05-01 00:00:00 | 2016-06-30 00:00:00  | 6  | 
| Auction 1 | 2016-05-01 00:00:00 | 2016-06-30 00:00:00  | 7  | 
| Auction 2 | 2016-06-01 00:00:00 | 2016-07-30 00:00:00  | 8  | 
| Auction 2 | 2016-06-01 00:00:00 | 2016-07-30 00:00:00  | 9  | 
| Auction 2 | 2016-06-01 00:00:00 | 2016-07-30 00:00:00  | 10  | 
+--------------+---------------------+---------------------------+---------+ 

下面的查詢得到我當前的拍賣:

mysql> select auction_name, auction_startdate, auction_planned_closedate from auctions where now() >= auction_startdate and now() <= auction_planned_closedate; 
+--------------+---------------------+---------------------------+ 
| auction_name | auction_startdate | auction_planned_closedate | 
+--------------+---------------------+---------------------------+ 
| Auction 1 | 2016-05-01 00:00:00 | 2016-06-30 00:00:00  | 
| Auction 2 | 2016-06-01 00:00:00 | 2016-07-30 00:00:00  | 
+--------------+---------------------+---------------------------+ 

,然後我做一個內部的「地段」表連接:

select auction_name, auction_startdate, auction_planned_closedate, lots.id 
from auctions 
where now() >= auction_startdate 
    and now() <= auction_planned_closedate 
inner join lots on auctions.id = lots.auction_id; 

ERROR 1064(42000):您有一個呃在您的SQL語法中使用ror;檢查對應於你的MySQL服務器版本正確的語法 在 線附近使用「上auctions.id = lots.auction_id內加入大量的」 1

我得到一個語法錯誤 手冊,該手冊我瞎了一會兒。

回答

2

錯誤的順序,把WHERE子句JOIN後:

select auction_name, auction_startdate, auction_planned_closedate, lots.id 
from auctions 
inner join lots on auctions.id = lots.auction_id 
where now() >= auction_startdate 
    and now() <= auction_planned_closedate 
0

join是錯誤的順序:

select a.auction_name, a.auction_startdate, a.auction_planned_closedate, l.id 
from auctions a inner join 
     lots l 
     on a.id = l.auction_id 
where now() >= a.auction_startdate and now() <= a.auction_planned_closedate ; 

注:

  • where那張from後條款。 join不是單獨的條款;它是from條款中的運營商。
  • 表別名使查詢更容易編寫和閱讀。
  • 限定所有列名稱。這就清楚了列的來源。
+0

有一次我輸入更快! – jarlh

+0

而且兩者都是正確的:-)快速的附加問題:在Gordon的答案中,他使用內部連接批次l。爲什麼額外的'l'? – wiwa1978

+0

@ wiwa1978,這是一個表別名。非常方便的方法來節省一些打字,在查詢中只需使用表別名而不是表名。 – jarlh