简体   繁体   中英

In mysql:how to display all column with distinct for one column

I want to display the column3 = 'ffff-jhj-01' with distinct values from column1.

Select distinct(name),phone,phone_id 
FROM `calldetails` 
where phone_id='ffff-jhj-01'

I have tried above query but it displays only column3 = 'ffff-jhj-01' not distinct of name.

If you want all columns with the distinct clause then try this query

Select distinct(name),phone,phone_id FROM `calldetails` 
where 
phone_id='ffff-jhj-01' 
GROUP BY name

Group by will give you distinct first column, but remember you will not get distinct data for column two.

For this case you don't even need the distinct clause you can get same output with Group by alone, like

Select name,phone,phone_id FROM `calldetails` 
where phone_id='ffff-jhj-01' 
Group by name 

output of both queries will be as follows

在此处输入图片说明

whereas if you go without Group by,

Select distinct(name),phone,phone_id FROM `calldetails` 
where 
phone_id='ffff-jhj-01' 

then output will be as follows

在此处输入图片说明

Moreover you can get all the variants details of phone,ie column 2 with the following query

SELECT name, GROUP_CONCAT( phone ) , phone_id
FROM  `calldetails` 
WHERE phone_id =  'ffff-jhj-01'
GROUP BY name
LIMIT 0 , 30

在此处输入图片说明

But the phone field should be a text/varchar field

尝试:

Select distinct(name),phone,phone_id FROM `calldetails` where phone_id='ffff-jhj-01'

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