0

i have the following table in phpmysql data base with three column , country , name and id(primary) show in html table one. it has multiple entry , i want to count the value and display it ( html table two.) what will be the mysql queries used for creating the second table

html table One

| id    | Country     | name |
|------:|------------:|-----:|
| 1     |USA          |   John
| 2     |USA          |   kAVI
| 3     | USA         |   Manoj
| 4     | USA         |   vijay
| 5     |Japan        |   Will
| 6     |Japan        |   Robert
| 7     |india        |   Vikas
| 8     |india        |   Monika
| 9     |india        |   Jecisa

html table two

|id   |Country|Count|
|----:|----..:|----:|
|1    |USA    |    4|
|2    |Japan  |    2|
|3    |india  |    3|

actual tablese

nbk
  • 45,398
  • 8
  • 30
  • 47
rajats
  • 11
  • 2

1 Answers1

0

Here's a simple solution using group by to achieve the count and row_number to generate the id.

with 
t as 
(select  country
        ,count(*) as count
        ,min(id) as id
from     html_table_One 
group by country
order by id) 

select row_number() over (order by id) as id
      ,country
      ,count
from   t
id country count
1 USA 4
2 Japan 2
3 india 3

Fiddle

DannySlor
  • 4,574
  • 1
  • 3
  • 11