Filtern Sie mehrere ID-Werte mit Navicat in der MySQL-Datenbank
P粉285587590
P粉285587590 2023-08-28 17:09:38
0
1
556
<p>Ich muss die Daten nach dem aktuellsten Ablaufdatum und Verkaufspreis filtern, ohne das id_product zu wiederholen. Ich habe versucht, dieses Problem zu lösen, kann aber nicht den richtigen Weg finden</p> <p>Dies ist die Navicat-Abfrage und die Ergebnisse</p> <pre class="brush:php;toolbar:false;">SELECT product_exp_date.idproduct, Produkt_Verfallsdatum.Verfallsdatum, Produkt_Verfallsdatum.Verkaufspreis AUS Produkt INNER JOIN Produkt_Verfallsdatum AN product.idproduct = product_exp_date.idproduct GRUPPIERE NACH product_exp_date.exp_date</pre> <pre class="brush:php;toolbar:false;">idproduct exp_dateselling_price 8 2022-11-01 300 5 08.06.2022 370 5 2022-06-09 350 7 2022-07-01 380 5 20.09.2022 450 6 08.10.2022 140 6 2023-06-08 150</pre> <p>Ich habe es auf diese Weise versucht</p> <pre class="brush:php;toolbar:false;">GROUP BY product_exp_date.idproduct</pre> <p>Aber es liefert mir andere Ergebnisse</p> <pre class="brush:php;toolbar:false;">idproduct exp_dateselling_price 5 2022-06-09 350 6 08.06.2023 150 7 2022-07-01 380 8 2022-11-01 300</pre> <p>Aber ich muss dieses Ergebnis bekommen</p> <pre class="brush:php;toolbar:false;">idproduct exp_dateselling_price 5 08.06.2022 370 6 08.10.2022 140 7 2022-07-01 380 8 2022-11-01 300</pre> <p>Produktliste</p> <pre class="brush:php;toolbar:false;">productid produktname 5A 6B 7 C 8 D</pre> <p>Product_EXP_DateTable</p> <pre class="brush:php;toolbar:false;">idproduct_exp_date idproduct exp_dateselling_price 1 5 09.06.2022 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 01.11.2022 300</pre> <p>Manchmal weist meine Anfrage Fehler auf, trotzdem benötige ich Hilfe, um dieses Problem zu lösen. Danke. </p>
P粉285587590
P粉285587590

Antworte allen(1)
P粉481035232

首先,让我纠正一下你的说法;那不是Navicat查询,那是一个MySQL查询。现在,这是两个不同的东西。MySQL是一个数据库,Navicat是一个工具 - 类似于MySQL Workbench、PHPMyAdmin或SQLyog等其他工具。它是为了让你可以通过图形界面执行数据库功能。

接下来,我将给出两个查询,你可以根据你的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表进行连接,以匹配这两个提取的值,以便我们可以提取selling_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(),然后将其作为子查询,并只添加一个WHERE子句。与之前的查询相比,这不需要JOIN操作。

你可以看到,我没有考虑到product表,因为我没有看到它在你的原始查询中被使用,但如果你确实需要将其连接起来,而且你无法弄清楚如何做到,只需给我留言,我会看看我能做什么。

演示fiddle

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!