How to select bi-directional mapped rows from a table in MySQL?

Ambrish Mayank

I have the MySQL table called table.

| ID     |item_code| item_name   |
| 1      | 12345   | abc         |
| 2      | 12345   | xyz         |
| 3      | 11221   | abc         |
| 4      | 19261   | www         |
| 5      | 12345   | abc         |
| 6      | 62898   | abc         |
| 7      | 92648   | xxx         |
| 8      | 45678   | xxx         | 
| 9      | 1234    | pqrs        | 
| 10     | 2345    | defg        | 
| 11     | 1234    | pqrs        |

I want the query which will results the below table:

>     |item_code | item_name   |
>     | 12345    | abc         |
>     | 12345    | xyz         |
>     | 11221    | abc         |
>     | 62898    | abc         |
>     | 92648    | xxx         |
>     | 45678    | xxx         |

I want the result considering of:

item_code mapped to multiple item_name. Here item_code 12345 is mapped with abc and xyz. item_code 1234 is mapped to pqrs.

item_name mapped to multiple item_code. Here item_name abc is mapped with three items: 12345,11221,62898. item_name xxx is mapped to 92648 and 45678.

Here is the SQL statement I tried:

(select a.item_code,a.item_name from table a
  join ( select item_code
           from table 
          group by item_code
         having count(*) > 1 ) b
    on a.item_code = b.item_code)
union
(select a.item_code,a.item_name from table a
  join ( select Item_name
           from table 
          group by Item_name
         having count(*) > 1 ) b
    on a.Item_name = b.Item_name);

But this SQL query does not generate the bi directional mapped keys. Can someone help me generate these bi-directional mappings?

William Price
SELECT DISTINCT t.item_code, t.item_name
FROM mytable t
INNER JOIN 
  (SELECT item_code, COUNT(DISTINCT item_name) num FROM mytable GROUP BY item_code) code
  ON (t.item_code = code.item_code)
INNER JOIN 
  (SELECT item_name, COUNT(DISTINCT item_code) num FROM mytable GROUP BY item_name) name
  ON (t.item_name = name.item_name)
WHERE code.num > 1 OR name.num > 1

The subqueries used in the two JOIN clauses are used to attach the counts for which each item code or name is repeated. The joined result set is filtered using the WHERE clause at the outermost level to include only those DISTINCT combinations where the code and/or the name was associated with multiple values for the opposite field.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Select and Delete exactly x rows from MySql table

From Dev

How to select non-matching rows from two mysql table

From Dev

MYSQL - How to UPDATE after SELECT from a table

From Dev

MySQL select row with two matching joined rows from another table

From Dev

Bi-directional reference from sub class

From Dev

How to select mapped data which are not in another table

From Dev

How to make a bi-directional RecyclerView?

From Dev

MySQL select and update multiple rows from same table

From Dev

mysql: select rows from another table as columns

From Dev

MySQL query to select rows from table 2 if *all* rows from table 1 are not present

From Dev

Postgres: delete duplicate rows with bi-directional relationship

From Dev

MySQL SELECT Substring of rows that do not exist from other TABLE

From Dev

How to select/match from MySQL table?

From Dev

PHP Select from MySQL table and return column with most rows

From Dev

How to select all rows from a table given conditions of rows?

From Dev

How to detect bi-directional link state

From Dev

How to select a timestamp from MySQL without rows

From Dev

How to select table in mysql with comparison by 2 rows using value from array?

From Dev

How do I select rows from my MySql table that occurred according to a max date?

From Dev

SQL - How to select discontinuous rows from a table with one select?

From Dev

mysql select from table where two (or more) rows match,

From Dev

How to represent bi-directional edges in graph

From Dev

How to get collection from both models in a bi-directional many-to-many relationship in django

From Dev

MySQL select grouping multiple rows from same table

From Dev

MySQL Select all rows from table 1 and all rows from table2 where

From Dev

How to establish a bi directional connection with ssl certificate?

From Dev

how to select one row from one table and multiple rows from other table using joins in mysql,

From Dev

Mysql: Select from parent table, only if child table has rows

From Dev

MYSQL: select all rows from table matching a combination and the opposite

Related Related

  1. 1

    Select and Delete exactly x rows from MySql table

  2. 2

    How to select non-matching rows from two mysql table

  3. 3

    MYSQL - How to UPDATE after SELECT from a table

  4. 4

    MySQL select row with two matching joined rows from another table

  5. 5

    Bi-directional reference from sub class

  6. 6

    How to select mapped data which are not in another table

  7. 7

    How to make a bi-directional RecyclerView?

  8. 8

    MySQL select and update multiple rows from same table

  9. 9

    mysql: select rows from another table as columns

  10. 10

    MySQL query to select rows from table 2 if *all* rows from table 1 are not present

  11. 11

    Postgres: delete duplicate rows with bi-directional relationship

  12. 12

    MySQL SELECT Substring of rows that do not exist from other TABLE

  13. 13

    How to select/match from MySQL table?

  14. 14

    PHP Select from MySQL table and return column with most rows

  15. 15

    How to select all rows from a table given conditions of rows?

  16. 16

    How to detect bi-directional link state

  17. 17

    How to select a timestamp from MySQL without rows

  18. 18

    How to select table in mysql with comparison by 2 rows using value from array?

  19. 19

    How do I select rows from my MySql table that occurred according to a max date?

  20. 20

    SQL - How to select discontinuous rows from a table with one select?

  21. 21

    mysql select from table where two (or more) rows match,

  22. 22

    How to represent bi-directional edges in graph

  23. 23

    How to get collection from both models in a bi-directional many-to-many relationship in django

  24. 24

    MySQL select grouping multiple rows from same table

  25. 25

    MySQL Select all rows from table 1 and all rows from table2 where

  26. 26

    How to establish a bi directional connection with ssl certificate?

  27. 27

    how to select one row from one table and multiple rows from other table using joins in mysql,

  28. 28

    Mysql: Select from parent table, only if child table has rows

  29. 29

    MYSQL: select all rows from table matching a combination and the opposite

HotTag

Archive