mysql - SQL查询: find lots that belong to current auction

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

然后我与“lots”表进行内部连接:

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): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'inner join lots on auctions.id=lots.auction_id' at line 1

我收到一个语法错误,我盲目地盯着它看了一会儿。

最佳答案

顺序错误,请将 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

关于mysql - SQL查询: find lots that belong to current auction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37747244/

相关文章:

mysql - 如何从数据库中按升序获取随机商店名称和价格?

mysql - 获取同一个表中的百分比

SQL Server 日期格式函数

c# - 在 LinQ Lambda 表达式中连接两个列值

php - 我可以在 PHP 中混合使用 MySQL API 吗?

mysql - 将自定义字段与表分开

php - 选择 DISTINCT 但回显全部?

MySQL,删除和索引提示

java - 像oracle中的搜索一样吗?

java - SQL 错误 :Column Index Out of Range, 2>1