mysql: select rows from another table as columns

mmrks6

is it somehow possible in mysql to get rows from a second table (content) to be displayed as columns in the first one (inventory)?

As the ID in the second table is not unique a JOIN produces duplicates, but i need all elements of the first table as a single row.

these are the tables: http://sqlfiddle.com/#!9/6da06/1

a result like this is needed:

id name      location   desc1  level1  desc2  level2  desc3  level3
1  test      somewhere  abc    20      def    50      ghi    30
2  anything  something  rfg    20      lzb    80      null   null
3  xxyzyzy   dffsdfd    atc    20      null   null    null   null

changes to the table structures are not possible, so is there any way how mysql can do that. there is no limit on how much duplicates of id can occur in table content... every row of inventory is needed, no duplicates. would be very cool, if sql can do something, or merging these arrays in PHP ?

Giorgos Betsos

If there is a limited number of content rows associated with each inventory row, then you can use the following query:

SELECT i.`id`, i.`name`, i.`location`,
       MAX(CASE WHEN c.rn = 1 THEN c.`desc` END) AS desc1,
       MAX(CASE WHEN c.rn = 1 THEN c.`level` END) AS level1,
       MAX(CASE WHEN c.rn = 2 THEN c.`desc` END) AS desc2,
       MAX(CASE WHEN c.rn = 2 THEN c.`level` END) AS level2,
       MAX(CASE WHEN c.rn = 3 THEN c.`desc` END) AS desc3,
       MAX(CASE WHEN c.rn = 3 THEN c.`level` END) AS level3
FROM inventory AS i
LEFT JOIN ( 
  SELECT `id`, `desc`, `level`,
         @row_number := IF (@id <> id, 
                           IF (@id := id, 1, 1),
                           IF (@id := id, @row_number+1, @row_number+1)) AS rn
  FROM content 
  CROSS JOIN (SELECT @row_number := 0, @id := 0) AS vars
  ORDER BY `id`, `desc` 
) AS c ON i.`id` = c.`id`
GROUP BY i.id, i.name, i.location  

The above uses variables to assign incremental numbers to rows of content that belong to the same id slice. Conditional aggregation is then used to pivot correlated content rows.

Demo here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Fetch rows in MySQL which are not present in both columns from another table

From Dev

Fetch rows in MySQL which are not present in both columns from another table

From Dev

MySQL select row with two matching joined rows from another table

From Dev

Mysql select rows comaparing with another table records

From Dev

MySQL project design - conditionally select from one table based on rows from another select query

From Dev

Mysql query to get rows of a table as columns of another, with column names from third table

From Dev

Insert multiple rows from select into another table

From Dev

MySQL - Select multiple rows from one table whose IDs are stored in another table

From Dev

Select multiple columns from a table and insert data into another table in a different database in PHP-MySQL

From Dev

php mysql select set of columns from table one based on values of colons in another table

From Dev

Select columns from another select as rows for each column

From Dev

mysql - move rows from one table to another

From Dev

MYSQL - UPDATE multiple rows from another table

From Dev

MySQL - Join & Count rows from another table

From Dev

insert multiple rows mysql from another table

From Dev

MySQL: Update rows in table, from rows with matching key in another table

From Dev

MYSQL select from table and count from another

From Dev

Select rows not in another table

From Dev

select rows from table based on data from another table

From Dev

select rows from table based on data from another table

From Dev

Oracle Temporary table columns and data from another table's rows

From Dev

How to select all the SQL rows from a table (With a unique combination of two columns) where the same combination is not already in another table

From Dev

Select all columns from MySQL table

From Dev

mysql select columns and sum from other table

From Dev

Table rows into columns in mysql

From Dev

Delete rows in MySQL matching two columns in another table

From Dev

Delete rows in MySQL matching two columns in another table

From Dev

MySQL update with select from another table

From Dev

MySQL update with select from another table

Related Related

  1. 1

    Fetch rows in MySQL which are not present in both columns from another table

  2. 2

    Fetch rows in MySQL which are not present in both columns from another table

  3. 3

    MySQL select row with two matching joined rows from another table

  4. 4

    Mysql select rows comaparing with another table records

  5. 5

    MySQL project design - conditionally select from one table based on rows from another select query

  6. 6

    Mysql query to get rows of a table as columns of another, with column names from third table

  7. 7

    Insert multiple rows from select into another table

  8. 8

    MySQL - Select multiple rows from one table whose IDs are stored in another table

  9. 9

    Select multiple columns from a table and insert data into another table in a different database in PHP-MySQL

  10. 10

    php mysql select set of columns from table one based on values of colons in another table

  11. 11

    Select columns from another select as rows for each column

  12. 12

    mysql - move rows from one table to another

  13. 13

    MYSQL - UPDATE multiple rows from another table

  14. 14

    MySQL - Join & Count rows from another table

  15. 15

    insert multiple rows mysql from another table

  16. 16

    MySQL: Update rows in table, from rows with matching key in another table

  17. 17

    MYSQL select from table and count from another

  18. 18

    Select rows not in another table

  19. 19

    select rows from table based on data from another table

  20. 20

    select rows from table based on data from another table

  21. 21

    Oracle Temporary table columns and data from another table's rows

  22. 22

    How to select all the SQL rows from a table (With a unique combination of two columns) where the same combination is not already in another table

  23. 23

    Select all columns from MySQL table

  24. 24

    mysql select columns and sum from other table

  25. 25

    Table rows into columns in mysql

  26. 26

    Delete rows in MySQL matching two columns in another table

  27. 27

    Delete rows in MySQL matching two columns in another table

  28. 28

    MySQL update with select from another table

  29. 29

    MySQL update with select from another table

HotTag

Archive