我希望在连接mysql时有明显和非独特的记录

时间:2018-04-10 12:20:54

标签: mysql sql sql-server

SELECT  distinct referrals.listing_id,submissions.listing_name
FROM submissions
INNER JOIN referrals ON referrals.listing_id=submissions.listing_id;

如果我这样做

SELECT  distinct referrals.listing_id,submissions.listing_name,referrals.timestamp
FROM submissions
INNER JOIN referrals ON referrals.listing_id=submissions.listing_id;

它为我提供了许多其他没有distinct listing_id

的记录

我希望distinct listing_id有他们的时间戳(这些时间戳不同,但是根据distinct listing_id

1 个答案:

答案 0 :(得分:3)

如果您希望每个列表包含一行,请使用聚合。例如:

GROUP BY
    Treatment.Treatment_Date, 
    Salutation.Value,
    Customer.Firstname,
    Customer.Lastname,
    Address.Value, 
    Postal.Postal, 
    Postal.City, 
    Patient.Name,
    TreatmentType.Type,
    Treatment.Price,
    Tax.Value

SELECT r.listing_id, s.listing_name, max(r.timestamp) as most_recent_timestamp FROM submissions s INNER JOIN referrals r ON r.listing_id = s.listing_id GROUP BY r.listing_id, s.listing_name; 适用于SELECT DISTINCT

中的所有表达式