I want to group based on one column of mysql and join with another table after getting the number.

Asked 1 years ago, Updated 1 years ago, 102 views

I made a GROUP based on ID in Table A.


SELECT TOP 200 A.ID,COUNT(A.ID) AS CNT

  FROM A  A
  LEFT join ANAME as Aname on A.ID = Aname.ID
  LEFT join BNAME as Bname on Aname.SEQ = Bname.SEQ
  where A.POI_ID > 1

In this way, we obtained A.ID and the number of each ID for the top 200 of A table as CNT.

I would like to join the Aname, Bname table based on this A.ID.

A.ID, A.CNT, Aname.NAME, Bname. I want these four dates to come out.

SELECT FROM (SELECT ~) and I found that the A.IDs were duplicated.

A table
--------------------
ID|Region

| Seoul

2|Incheon

3|A game

1|SEOUL

2|INCHEON
Aname table
---------------------
ID|name

1|Korea

2|Republic of Korea

3|Paris

4|North Korea
Bname Table
---------------------
ID|Subscription date

1|200101

2|990101

3|901007
The desired value
-----------
ID|CNT|NAME|Join date

1|2|Daehan|200101

2|2|Minguk|990101

3|1|North Korea|901007

A. You need to find the number of duplicate IDs based on ID, and only pick the ID that does not overlap and JOIN. Please help me.

mysql sql querying group-by

2022-09-20 20:54

1 Answers

SELECT TOP 200 A.ID,COUNT(A.ID) AS CNT

  FROM A  A
  LEFT join ANAME as Aname on A.ID = Aname.ID
  LEFT join BNAME as Bname on Aname.SEQ = Bname.SEQ
  where A.POI_ID > 1

Don't I get only 1 rows if I turn this query?

SELECT A.ID, COUNT(A.ID) AS CNT, Aname.name, Bname. Date of subscription
  FROM A  A
  LEFT join ANAME as Aname on A.ID = Aname.ID
  LEFT join BNAME as Bname on Aname.SEQ = Bname.SEQ
  where A.POI_ID > 1 group by a.id;

If there is a duplicate value, it will be based on the value in the first row.


2022-09-20 20:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.