使用 Navicat 过滤 MySQL 数据库中的多个 id 值
我需要用最近的到期日期和销售价格过滤数据,而不重复 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,让我纠正你;这不是 Navicat 查询,而是 MySQL 查询。现在,这是两件不同的事情。 MySQL 是一个数据库,Navicat 是一个工具 - 相当于 MySQL Workbench、PHPMyAdmin 或 SQLyog 等其他工具。它是为了让您可以通过 GUI 执行数据库功能。
接下来,我将提供两个查询,您可以根据您的 MySQL 版本使用它们。第一个是:
您应该能够在任何版本的 MySQL 或 MariaDB 中运行上面的查询。想法是通过
idproduct
获取最小的exp_date
组,然后将其作为子查询,再次将其与product
表连接以匹配这两个提取的值以便我们可以提取sales_price
。第二个查询:
只能在支持窗口函数的 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:
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 byidproduct
, then make it as a sub-query, join it withproduct
table again to match those two extracted value so that we can extract theselling_price
.The second query:
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 aWHERE
. Compared to the query before, this doesn't requireJOIN
.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