简体   繁体   English

如何在MySQL中获取具有最小值的对应ID?

[英]How to get corresponding id with minimum value of a row in MySQL?

I have two tables named deals and mp_details . 我有两个表命名为dealsmp_details Columns deal_id and mp_details_id are the two primary keys. deal_idmp_details_id是两个主键。 There is a relation between the two tables using the deal_mp_id column where it works as a foreign key. 两个表之间的关系使用deal_mp_id列作为外键。 I want to fetch all the minimum records of column name deal_selling_price with group by mp_details_id . 我想取列名的所有最低记录deal_selling_price用组mp_details_id Here are the tables: 表格如下:

deals 交易

╔═════════╦════════════╦════════════════════╗
║ deal_id ║ deal_mp_id ║ deal_selling_price ║
╠═════════╬════════════╬════════════════════╣
║    3    ║     1      ║        425         ║
║    4    ║     1      ║        540         ║  
║    5    ║     2      ║        340         ║
║    6    ║     2      ║        315         ║
║    7    ║     3      ║        425         ║
║    8    ║     3      ║        720         ║
║    9    ║     4      ║        382.5       ║
║   10    ║     4      ║        495         ║
║   11    ║     5      ║        595         ║
║   12    ║     5      ║        720         ║
╚═════════╩════════════╩════════════════════╝

mp_details mp_details

╔═══════════════╦═════════════════════╗
║ mp_details_id ║   mp_details_name   ║
╠═══════════════╬═════════════════════╣
║       1       ║ Olive Bar & Kitchen ║
║       2       ║ Thai High           ║
║       3       ║ Tonino              ║
║       4       ║ Impromptu           ║
║       5       ║ Zerruco             ║
╚═══════════════╩═════════════════════╝

And I want this kind of result: 我想要这样的结果:

╔═════════╦════════════╦════════════════════╗
║ deal_id ║ deal_mp_id ║ deal_selling_price ║
╠═════════╬════════════╬════════════════════╣
║    3    ║     1      ║        425         ║
║    6    ║     2      ║        315         ║
║    7    ║     3      ║        425         ║
║    9    ║     4      ║        382.5       ║
║   11    ║     5      ║        595         ║
╚═════════╩════════════╩════════════════════╝

The following query will give you the result set from your screen capture above: 以下查询将为您提供上述屏幕截图中的结果集:

SELECT d1.deal_id, d1.deal_mp_id, d2.minPrice
FROM deals d1
INNER JOIN
(
    SELECT deal_mp_id, MIN(deal_selling_price) AS minPrice
    FROM deals
    GROUP BY deal_mp_id
) d2
    ON d1.deal_mp_id = d2.deal_mp_id AND d1.deal_selling_price = d2.minPrice

If you also want to include information from the mp_details table, you can do an additional join like this: 如果您希望包含mp_details表中的信息,则可以执行以下附加mp_details

SELECT d1.deal_id, d1.deal_mp_id, d2.minPrice, mp.mp_details_name
FROM deals d1
INNER JOIN
(
    SELECT deal_mp_id, MIN(deal_selling_price) AS minPrice
    FROM deals
    GROUP BY deal_mp_id
) d2
    ON d1.deal_mp_id = d2.deal_mp_id AND d1.deal_selling_price = d2.minPrice
INNER JOIN mp_details mp
    ON d1.deal_mp_id = mp.mp_details_id

Follow the link below for a running demo: 请点击以下链接以获取正在运行的演示:

SQLFiddle SQLFiddle

Try below query 尝试以下查询

select 
    D.Deal_id, MD.mp_details_id, MIN(D.deal_selling_price) 
FROM
    deals D inner join  mp_details MD 
ON
    D.Deal_id = MD.mp_details_id 
GROUP BY 
   D.Deal_id, MD.mp_details_id

Use this query: 使用此查询:

SELECT d.deal_id, d.deal_mp_id, d.deal_selling_price
FROM deals d
JOIN
(
  SELECT deal_mp_id, MIN(deal_selling_price) AS min_selling_price
  FROM deals
  GROUP BY deal_mp_id
) m
ON d.deal_mp_id = m.deal_mp_id AND d.deal_selling_price = m.min_selling_price

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM