join two table and count

cangak

i have this kind of structure

kode data

+------------+-----------+
| code | ket             |
+------------+-----------+
|20401 | code one        |
|21401 | code two        |
|22401 | code three      |
|etc   | etc             |
+------------------------+
+-----+-----------------+-----------+
| id  | code1           | code2     |
+-----+-----------------+-----------+
|1    | 20401           | 21402     |
|2    | 21401           | 22401     |
|3    | 22401           | 20401     |
|4    | 20401           | 21401     |
+-----------------------+-----------+

how tow count and sum to achieve output like below

+-----------+-------------+-------------+ |code | count code1 | count code2 | +-----------+-------------+-------------+ |code one | 2 | 1 | |code two | 1 | 1 | |code three | 3 | 1 | |etc | etc | etc | +-----------+-------------+-------------+

basicly what i want is select table ket and count in code1 and code2 that hace the code with one query.

Mureinik

You could perform two joins:

SELECT    ket AS code, 
          cnt1 AS "count code1"
          cnt2 AS "count code2"
FROM      kode
LEFT JOIN (SELECT   code1, COUNT(*) AS cnt1
           FROM     data
           GROUP BY code1) c1 ON kode.code = c1.code1
LEFT JOIN (SELECT   code2, COUNT(*) AS cnt2
           FROM     data
           GROUP BY code2) c2 ON kode.code = c2.code2

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

MYSQL select count two table left join

分類Dev

Incorrect count value in subquery on table join column

分類Dev

mysql join 3 table but the "COUNT" be duplicate

分類Dev

How to join the two table in Laravel 5.4

分類Dev

how to join two table with where to one column

分類Dev

Count including 0 on join two tables with different rows postgres

分類Dev

SQL Join same table on different condition and get count

分類Dev

How can I INNER JOIN another two columns to a table?

分類Dev

Join Two Pivot Table and obtain multi value per cell in panda

分類Dev

Left join two tables, and then append only new rows to table

分類Dev

Two tables, join not returning data from one table

分類Dev

Query table data with inner join and two foreing keys

分類Dev

Delete from one table using join on two tables

分類Dev

How will i join/combine these two table to get one result

分類Dev

Conditional join to two different tables based on 2 columns in 1 table

分類Dev

How to join and total two different tables into a third table

分類Dev

How to "count" two different criteria from the same column in a table?

分類Dev

How to create a count table of two variables in excel pivot

分類Dev

Join two data.tables on date, with closest date in table 1 strictly less than date in second table

分類Dev

MySQL two-table INNER JOIN , LEFT JOINED onto third table with only one row with lowest value

分類Dev

LINQ, Join, GroupBy and Count

分類Dev

SQLite COUNT JOIN DISTINCT

分類Dev

Oracle: How do I join two result set queries (based on the same table) and then subtract the results?

分類Dev

How to perform reduce side join on Java Mapreduce with two table which have many-to-many relationship?

分類Dev

Trying to join two tables on a zip - one table has no leading zeros, the other has leading zeros

分類Dev

Inner Join on two Fields

分類Dev

Join two data frames

分類Dev

Join two IOs with - in haskell

分類Dev

join two tables in mysql

Related 関連記事

  1. 1

    MYSQL select count two table left join

  2. 2

    Incorrect count value in subquery on table join column

  3. 3

    mysql join 3 table but the "COUNT" be duplicate

  4. 4

    How to join the two table in Laravel 5.4

  5. 5

    how to join two table with where to one column

  6. 6

    Count including 0 on join two tables with different rows postgres

  7. 7

    SQL Join same table on different condition and get count

  8. 8

    How can I INNER JOIN another two columns to a table?

  9. 9

    Join Two Pivot Table and obtain multi value per cell in panda

  10. 10

    Left join two tables, and then append only new rows to table

  11. 11

    Two tables, join not returning data from one table

  12. 12

    Query table data with inner join and two foreing keys

  13. 13

    Delete from one table using join on two tables

  14. 14

    How will i join/combine these two table to get one result

  15. 15

    Conditional join to two different tables based on 2 columns in 1 table

  16. 16

    How to join and total two different tables into a third table

  17. 17

    How to "count" two different criteria from the same column in a table?

  18. 18

    How to create a count table of two variables in excel pivot

  19. 19

    Join two data.tables on date, with closest date in table 1 strictly less than date in second table

  20. 20

    MySQL two-table INNER JOIN , LEFT JOINED onto third table with only one row with lowest value

  21. 21

    LINQ, Join, GroupBy and Count

  22. 22

    SQLite COUNT JOIN DISTINCT

  23. 23

    Oracle: How do I join two result set queries (based on the same table) and then subtract the results?

  24. 24

    How to perform reduce side join on Java Mapreduce with two table which have many-to-many relationship?

  25. 25

    Trying to join two tables on a zip - one table has no leading zeros, the other has leading zeros

  26. 26

    Inner Join on two Fields

  27. 27

    Join two data frames

  28. 28

    Join two IOs with - in haskell

  29. 29

    join two tables in mysql

ホットタグ

アーカイブ