bigjoy10
bigjoy10

Reputation: 55

Mysql group by some field non distinct

I have some task. I need to get this table. It consist of two tables. where table_2.name not distinct. Please help me to make this query. Thanks!

id   name1  id  name2
1    Alex    2  Alexander
2    Alex    3  Alexan
4    Vlad    5  Vladimir
5    Vlad    6  Vladik

From two tables.

Table_1
id   name
1     Alex
2     Pit
3     Vlad

And Table_2

   id id_table_1 real_name
   1      1     Alexander
   2      1     Alexan
   3      2     Piter
   4      3     Vladimir
   5      3     Vladik

my query

select table_1.name,table_2.id,table_2.real_name
from table_1 join table_2
where table_1.id = table_2.id_table_1

Upvotes: 2

Views: 500

Answers (1)

munsifali
munsifali

Reputation: 1732

if all you want is to combine duplicated rows, use SELECT DISTINCT.

If you need to combine rows that are duplicate in some columns, use GROUP BY but you need to to specify what to do with the other columns. You can either omit them (by not listing them in the SELECT clause) or aggregate them (using functions like SUM, MIN, and AVG)

Upvotes: 1

Related Questions