mysql - 购物车 - 左连接 - 希望限制为最接近购买日期的一行商品

标签 mysql sql

我正在尝试向客户展示 - 所有之前购买的商品

我有 3 张 table :

shopping_cart
shopping_orders
audit_shopping_items

我想从 -audit_shopping_items 表中选择最接近苹果购买日期(订单日期)的苹果价格

问题:问题是当我执行 LEFT JOIN 时,我在同一订单 ID 中得到 2 行苹果,代码如下

我只想要一个,最接近产品购买日期的

表:audit_shopping_items

+-----+------------+--------------+--------------+-------------------+----------------+---------------------+--------------+
| id  | item_code  |  item_name   | item_weight  | item_weight_unit  | item_quantity  | item_selling_price  | item_status  |
+-----+------------+--------------+--------------+-------------------+----------------+---------------------+--------------+
|   1 |  200000001 | apple        |          500 | gm                |              1 |                  10 | active       |
|   2 |  200000002 | apple_green  |          500 | gm                |              1 |                  10 | active       |
|   3 |  200000003 | avocado      |          500 | gm                |              1 |                  10 | active       |
|   4 |  200000001 | apple        |          500 | gm                |              1 |                  18 | active       |
+-----+------------+--------------+--------------+-------------------+----------------+---------------------+--------------+

我的Mysql查询

SELECT 
  sc.item_code, 
  so.order_id, 
  si.item_name, 
  si.item_selling_price, 
FROM 
  shopping_cart as sc 
  LEFT JOIN shopping_orders AS so ON sc.order_id = so.order_id 
  LEFT JOIN audit_shopping_items AS si ON si.item_code (
    SELECT 
      pi.item_code 
    FROM 
      shopping_cart AS pi 
    WHERE 
      pi.sys_m_date <= so.sys_m_date 
      AND pi.item_code = sc.item_code 
    LIMIT 
      1
  ) 
LIMIT 
  500

表:shopping_cart

+-----+----------------------+----------------------+------------+----------------+-------------+-
| id  |     sys_c_date       |     sys_m_date       | item_code  | item_quantity  |  order_id   | 
+-----+----------------------+----------------------+------------+----------------+-------------+-
|  1  | 2019-12-09 22:05:05  | 2019-12-09 22:05:15  | 200000001  |             2  | 1869460212  | 
|  2  | 2019-12-09 22:05:08  | 2019-12-09 22:05:15  | 200000003  |             2  | 1869460212  | 
|  3  | 2019-12-09 22:06:16  | 2019-12-09 22:06:24  | 200000001  |             4  | 2891252193  | 
|  4  | 2019-12-09 22:06:18  | 2019-12-09 22:06:24  | 200000004  |             2  | 2891252193  | 
+-----+----------------------+----------------------+------------+----------------+-------------+-

表:shopping_orders

+-----+----------------------+----------------------+-------------+---------------+------------------+----------------+--------------+
| id  |     sys_c_date       |     sys_m_date       |  order_id   | order_amount  |   order_owner    | order_status   | order_method |
+-----+----------------------+----------------------+-------------+---------------+------------------+----------------+--------------+
|  1  | 2019-12-09 22:05:15  | 2019-12-09 22:05:15  | 1869460212  |           40  | abc@example.com  | created        | cod          |
|  2  | 2019-12-09 22:06:24  | 2019-12-09 22:06:24  | 2891252193  |           92  | abc@example.com  | created        | cod          |
+-----+----------------------+----------------------+-------------+---------------+------------------+----------------+--------------+

最佳答案

左连接audit_shopping_items AS si ON -- si.id -- 而不是 -- si.item_code -- 到 pi-id 解决了问题

            SELECT 
              sc.item_code, 
              so.order_id, 
              si.item_name, 
              si.item_selling_price, 
            FROM 
              shopping_cart as sc 
              LEFT JOIN shopping_orders AS so ON sc.order_id = so.order_id 
              LEFT JOIN audit_shopping_items AS si ON si.id (
                SELECT 
                  pi.id 
                FROM 
                  shopping_cart AS pi 
                WHERE 
                  pi.sys_m_date <= so.sys_m_date 
                  AND pi.item_code = sc.item_code 
                LIMIT 
                  1
              ) 
            LIMIT 
              500

关于mysql - 购物车 - 左连接 - 希望限制为最接近购买日期的一行商品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59265922/

相关文章:

mysql - Xeround 与 ClearDB 托管的 MySQL 优缺点?

php - 如何检索存储在表中的sql查询并为表中的所有查询创建动态php页面

php - 如何添加、编辑和删除数据库的逗号分隔值。?

mysql - “不完整”where 子句仍在运行

mysql - 使用 select 更新数据表

sql - 用c#在sql中插入q记录?

java - javax.sql 和 java.sql 之间的区别?

python - Django、MySQL 和 Cloud9

php - 使用 PHP 创建 CSV 下载链接

java - 在 Oracle 中设置时间戳