Select distinct records on a join

前端 未结 4 1597
情深已故
情深已故 2020-12-05 08:18

I have two mysql tables - a sales table:

+----------------+------------------------------+------+-----+---------+-------+
| Field          | Type                    


        
相关标签:
4条回答
  • 2020-12-05 08:27

    YOu had comment about the sales week too. And wanting the most recent week, you may want to try using a GROUP BY

    SELECT 
          items.ItemName, 
          items.ItemId,
          max( Sales.SaleWeek ) MostRecentSaleWeek
       FROM 
          items JOIN sales ON items.ItemId = sales.ItemId
       WHERE 
          sales.StoreID = ? 
       GROUP BY
          items.ItemID,
          items.ItemName
       ORDER BY
          MostRecentSaleWeek,   -- ordinal column number 3 via the MAX() call
          items.ItemName
    

    You may have to change the ORDER BY to the ordinal 3rd column reference if you so want based on that column.. This query will give you each distinct item AND the most recent week it was sold.

    0 讨论(0)
  • 2020-12-05 08:33

    We can use this:

    INSERT INTO `test_table` (`id`, `name`) SELECT DISTINCT 
        a.`employee_id`,b.`first_name` FROM `employee_leave_details`as a INNER JOIN 
        `employee_register` as b ON a.`employee_id` = b.`employee_id`
    
    0 讨论(0)
  • 2020-12-05 08:43

    A DISTINCT should do what you're looking for:

    SELECT DISTINCT items.ItemName, items.ItemId FROM items
    JOIN sales ON items.ItemId = sales.ItemId 
    WHERE sales.StoreID = ? ORDER BY sales.SaleWeek DESC;
    

    That would return only distinct items.ItemName, items.ItemId tuples.

    0 讨论(0)
  • 2020-12-05 08:44
        SELECT  u.user_name,u.user_id, u.user_country,u.user_phone_no,ind.Industry_name,inv.id,u.user_email
        FROM invitations inv
        LEFT JOIN users u
        ON inv.sender_id = u.user_id
        LEFT JOIN employee_info ei
        ON inv.sender_id=ei.employee_fb_id   
        LEFT JOIN industries ind
        ON ei.industry_id=ind.id
        WHERE inv.receiver_id='XXX'
        AND inv.invitation_status='0'
        AND inv.invitati
    
    on_status_desc='PENDING'
    GROUP BY (user_id)
    
    0 讨论(0)
提交回复
热议问题