使用 Navicat 过滤 MySQL 数据库中的多个 id 值

发布于 2025-01-09 17:38:57 字数 1802 浏览 0 评论 0原文

我需要用最近的到期日期和销售价格过滤数据,而不重复 id_product,我尝试解决这个问题,但我无法找到正确的方法来做到这一点,

这是 Navicat 查询和结果

SELECT
    product_exp_date.idproduct, 
    product_exp_date.exp_date, 
    product_exp_date.selling_price
FROM
    product
    INNER JOIN
    product_exp_date
    ON 
        product.idproduct = product_exp_date.idproduct
GROUP BY
    product_exp_date.exp_date
idproduct  exp_date   selling_price
8         2022-11-01      300
5         2022-06-08      370
5         2022-06-09      350
7         2022-07-01      380
5         2022-09-20      450
6         2022-10-08      140
6         2023-06-08      150

我已经尝试过这种方式

GROUP BY
    product_exp_date.idproduct

但它给了我不同的结果

idproduct  exp_date   selling_price
5         2022-06-09    350
6         2023-06-08    150
7         2022-07-01    380
8         2022-11-01    300

,但我需要得到这个结果

idproduct  exp_date   selling_price
5         2022-06-08      370
6         2022-10-08      140
7         2022-07-01      380
8         2022-11-01      300

PRODUCT TABLE

productid  product_name 
5               A     
6               B     
7               C     
8               D     

PRODUCT_EXP_DATE TABLE

idproduct_exp_date  idproduct   exp_date   selling_price
      1               5        2022-06-09    350
      2               6        2023-06-08    150
      3               5        2022-06-08    370
      4               5        2022-09-20    450
      5               6        2022-10-08    140
      6               7        2022-07-01    380
      7               8        2022-11-01    300              

有时我的查询有一些错误,无论如何我需要帮助来解决这个问题, 谢谢。

I need to filter the data with the nearest exp-date and selling-price, without repeating id_product, I try to resolve this problem, but I can't get a proper way to doit

this is Navicat query and result

SELECT
    product_exp_date.idproduct, 
    product_exp_date.exp_date, 
    product_exp_date.selling_price
FROM
    product
    INNER JOIN
    product_exp_date
    ON 
        product.idproduct = product_exp_date.idproduct
GROUP BY
    product_exp_date.exp_date
idproduct  exp_date   selling_price
8         2022-11-01      300
5         2022-06-08      370
5         2022-06-09      350
7         2022-07-01      380
5         2022-09-20      450
6         2022-10-08      140
6         2023-06-08      150

I already tried this way

GROUP BY
    product_exp_date.idproduct

but it's given me different result

idproduct  exp_date   selling_price
5         2022-06-09    350
6         2023-06-08    150
7         2022-07-01    380
8         2022-11-01    300

but I need to get this result

idproduct  exp_date   selling_price
5         2022-06-08      370
6         2022-10-08      140
7         2022-07-01      380
8         2022-11-01      300

PRODUCT TABLE

productid  product_name 
5               A     
6               B     
7               C     
8               D     

PRODUCT_EXP_DATE TABLE

idproduct_exp_date  idproduct   exp_date   selling_price
      1               5        2022-06-09    350
      2               6        2023-06-08    150
      3               5        2022-06-08    370
      4               5        2022-09-20    450
      5               6        2022-10-08    140
      6               7        2022-07-01    380
      7               8        2022-11-01    300              

sometimes's my query has some error, anyway I need help to resolve this problem,
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滿滿的愛 2025-01-16 17:38:57

首先,让我纠正你;这不是 Navicat 查询,而是 MySQL 查询。现在,这是两件不同的事情。 MySQL 是一个数据库,Navicat 是一个工具 - 相当于 MySQL Workbench、PHPMyAdmin 或 SQLyog 等其他工具。它是为了让您可以通过 GUI 执行数据库功能。

接下来,我将提供两个查询,您可以根据您的 MySQL 版本使用它们。第一个是:

SELECT p1.idproduct,
       p1.exp_date,
       p1.selling_price
FROM product_exp_date p1 
JOIN (
   SELECT idproduct,
       MIN(exp_date) AS minexp
   FROM product_exp_date
   GROUP BY idproduct
  ) AS p2
 ON p1.idproduct=p2.idproduct 
 AND p1.exp_date=p2.minexp
ORDER BY p1.idproduct;

您应该能够在任何版本的 MySQL 或 MariaDB 中运行上面的查询。想法是通过idproduct获取最小的exp_date组,然后将其作为子查询,再次将其与product表连接以匹配这两个提取的值以便我们可以提取sales_price

第二个查询:

SELECT idproduct,
       exp_date,
       selling_price
FROM (
   SELECT idproduct,
          exp_date,
          selling_price,
          ROW_NUMBER() OVER (PARTITION BY idproduct ORDER BY exp_date) rn
   FROM product_exp_date
  ) AS p1
WHERE rn=1;

只能在支持窗口函数的 MySQL v8+ 或 MariaDB 10.2+(及更高版本)上运行。这个想法与之前的查询有点不同,在这里,我们将专注于根据特定条件生成 ROW_NUMBER(),然后将其作为子查询并仅添加 哪里。与之前的查询相比,这不需要 JOIN

正如您所看到的,我没有考虑 product 表,因为我没有看到它在原始查询中的任何地方使用,但如果您确实需要加入它,并且您不能弄清楚如何做,只需给我发表评论,我会看看我能做什么。

演示小提琴

First of all, let me correct you; that is not a Navicat query, that's a MySQL query. Now, that's two different thing. MySQL is a database and Navicat is a tool - equivalent to other tools like MySQL Workbench, PHPMyAdmin or SQLyog. It is made so that you can do database functions through GUI.

Next is, I'm going to give two queries that you can use depending on your MySQL version. The first one is this:

SELECT p1.idproduct,
       p1.exp_date,
       p1.selling_price
FROM product_exp_date p1 
JOIN (
   SELECT idproduct,
       MIN(exp_date) AS minexp
   FROM product_exp_date
   GROUP BY idproduct
  ) AS p2
 ON p1.idproduct=p2.idproduct 
 AND p1.exp_date=p2.minexp
ORDER BY p1.idproduct;

You should be able to run the query above in any version of MySQL or MariaDB. The idea is to get the smallest exp_date group by idproduct, then make it as a sub-query, join it with product table again to match those two extracted value so that we can extract the selling_price.

The second query:

SELECT idproduct,
       exp_date,
       selling_price
FROM (
   SELECT idproduct,
          exp_date,
          selling_price,
          ROW_NUMBER() OVER (PARTITION BY idproduct ORDER BY exp_date) rn
   FROM product_exp_date
  ) AS p1
WHERE rn=1;

This can only run on MySQL v8+ or MariaDB 10.2+ (and above) that support window function. The idea is a little different from the previous query whereby here, we'll focus on generating the ROW_NUMBER() based on specific conditions, then make that as a sub-query and only add a WHERE. Compared to the query before, this doesn't require JOIN.

As you can see, I did not take the product table into consideration since I don't see it being use anywhere in your original query, but if you do require to join it, and you can't figure out how, just drop me a comment and I'll see what I can do.

Demo fiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文