sql查询以获取带有折扣的季节价格的产品

时间:2018-09-25 12:49:30

标签: mysql sql

我有这个季节表

+----+------------+------------+------------+-------+------+----------+------------+------------+------------+
| id | base_price | startDate  | endDate    | cost  | type | Duration | product_id | created_at | updated_at |
+----+------------+------------+------------+-------+------+----------+------------+------------+------------+
|  1 |     100.00 | 1537390800 | 1538773200 | 95.00 | day  |        2 |          9 | NULL       | NULL       |
|  2 |     100.00 | 1537390800 | 1538773200 | 85.00 | day  |        3 |          9 | NULL       | NULL       |
|  3 |     100.00 | 1537390800 | 1538773200 | 75.00 | day  |        4 |          9 | NULL       | NULL       |
|  4 |     100.00 | 1537390800 | 1538773200 | 70.00 | day  |        5 |          9 | NULL       | NULL       |
+----+------------+------------+------------+-------+------+----------+------------+------------+------------+

和产品表

+----+----------------------------+-----------+-----+-------+---------+--------------+---------------------+---------------------+---------------------+
| id | alias                      | status    | img | price | user_id | published_at | deleted_at          | created_at
 | updated_at          |
+----+----------------------------+-----------+-----+-------+---------+--------------+---------------------+---------------------+---------------------+
|  8 | toyota-corolla-1-6-elegant | draft     | 18  | 30    |       1 | 2018-08-14   | NULL                | 2018-08-14 15:06:12 | 2018-08-20 14:58:18 |
|  9 | test                       | published |     | 0     |       1 | 2018-08-23   | 2018-09-10 19:44:29 | 2018-08-23 14:45:18 | 2018-09-10 19:44:29 |
+----+----------------------------+-----------+-----+-------+-------

现在我尝试这个sql

SELECT *
FROM products, 
(SELECT *
FROM  season
WHERE Duration =  ( SELECT MAX(Duration) FROM ec_season WHERE `Duration` <= 3)
) as t1
LEFT OUTER JOIN t1
ON t1.product_id = products.id

但是它给了我

1146-表'****。t1'不存在

如何将产品附加到t1?

1 个答案:

答案 0 :(得分:3)

尝试一下:您的连接语法不正确

SELECT *
FROM products left outer join
(SELECT *
FROM  season
WHERE Duration =  ( SELECT MAX(Duration) FROM ec_season WHERE `Duration` <= 3)
) as t1 ON t1.product_id = products.id