简体   繁体   中英

SQL left join where the joined table only displays the row column with lowest figure

My current join with 3 tables works. The 2nd table contains numbers, which are prices example: 800. It contains several prices for the same item and I only want to display the lowest found related to the customers datatabase. So I want to join the 2nd table on the condition that it only displays the row with the lowest number ONLY no others rows. I need some advice please thanks

The Table/column that contains the price is called "item.amount" and the table is called "item".

$sql = "SELECT
customers.id, customers.name,
item.id, item.amount, fav.id

FROM customers

LEFT JOIN item   ON customers.id = item.id 
LEFT JOIN fav    ON customers.id = fav.id ";

I have found the solution using MIN(Point) MinPoint. This example explains and can be used with LEFT JOIN

SELECT tbl.*
FROM TableName tbl
  INNER JOIN
  (
    SELECT Id, MIN(Point) MinPoint
    FROM TableName
    GROUP BY Id
  ) tbl1
  ON tbl1.id = tbl.id
WHERE tbl1.MinPoint = tbl.Point

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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