简体   繁体   中英

MySQL select distinct values based on specific field

hi i have a table with 6 columns, amongst them the is a column called user_id which has many occurances in the table and also a column ie laptops where it references the laptops that a user has brought for some action. This also have many occurances in the table.

ID  DATE        LAPTOP    USER_ID
1   1/1/2014    Acer      10
2   1/1/2014    Toshiba   2
3   1/1/2014    HP        5
4   1/1/2014    Acer      5
5   1/1/2014    Toshiba   5
6   1/1/2014    HP        2
7   2/1/2014    Lenovo    2
8   2/1/2014    Acer      10
9   2/1/2014    HP        2
10  2/1/2014    HP        5

i need to select the distinct laptop occurences for each user_id and not generally the distinct occurances frm the table so the desired result would be

LAPTOP    USER_ID
Acer      10
Toshiba   2
HP        2
Lenovo    2
HP        5
Acer      5
Toshiba   5

whatever i ve tried seems to be far from the desired result so far

you can use GROUP BY

SELECT LAPTOP, USER_ID FROM Table1 GROUP BY LAPTOP, USER_ID;

just to make the answer as complete as possible, I am adding jpw's solution that is a comment above.

SELECT DISTINCT laptop, user_id FROM table

Here's a GROUP BY fiddle

Here's jpw's DISTINCT fiddle

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