SQL查询:查找属于当前拍卖的批次

时间:2016-06-10 11:34:36

标签: mysql 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_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       |
+--------------+---------------------+---------------------------+

然后我与&#39;批次&#39;进行内部联接。表:

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;
  

错误1064(42000):您的SQL语法有错误;检查   手册,对应右边的MySQL服务器版本   在auctions.id = lots.auction_id&#39;上使用靠近&#39;内部联接批次的语法在   第1行

我收到语法错误,我盯着他看了一会儿。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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子句中的运算符。
  • 表别名使查询更容易编写和阅读。
  • 限定所有列名称。这清楚地表明了列的来源。