Dynamic rows to columns from multiple tables with mysql

Chris

So I've been reading through some of the other questions about dynamic rows to columns. By adapting another query in another answer here

mysql select dynamic row values as column names, another column as value,

I can get it to work for a single table, however I need to also pull records from two other tables to include in the result. Given these tables:

Cars
ID    BRAND    NAME   etc1...
1     gmc      sierra
2     ford     ranger
3     dodge    dakota
4     kia      rio

Dice
ID    DESCRIPTION
1     blue
2     green
3     red
etc2. etc2.

Stock
ID    CAR_ID    DICE_ID    NUMBER
1     1         3          01V,3Y6
2     3         1          8Z4
3     2         2          03X
4     1         1          C7B

So yes this doesn't make much sense but it's only to show the structure. My result needs to come out looking like this:

CAR_ID    BRAND    NAME    etc1.    BLUE    GREEN    RED     etc2.
1         gmc      sierra  ...     C7B     null     01V,3Y6 ...
2         ford     ranger  ...     null    03X      null    ...
3         dodge    dakota  ...     8Z4     null     null    ...
4         kia      rio     ...     null    null     null    ...

Simple if it was static but the number of rows in Cars and Dice will be dynamic so it can't be hard-coded. I can get Dice to output the rows into columns but there are two things I can't figure out:

  • The main query is supposed to list all columns from the Cars table along with all rows as columns from the Dice table but I can't figure out how to attach the Dice query to the Cars query.
  • I need to pull the relative description from the Dice table to use as a column header. Using dice.id is not practical or helpful. I can't figure out how to incorporate this into my working query for the Dice table.

Does anyone know how to produce the desired results?

Edit 1: I forgot to say that the results must list ALL Cars whether or not they have Dice in stock.

Edit 2: I should have clarified that NUMBER meant product number and not count. This is a string field that can hold multiple product numbers. It's legacy data.

Saharsh Shah

Try this:

SET SESSION group_concat_max_len = 10000;
SELECT GROUP_CONCAT(DISTINCT CONCAT('MAX(IF(D.ID = ',D.ID,', D.ID, NULL)) as \'', D.DESCRIPTION, '\'')) INTO @sql FROM Dice D;

SET @s = CONCAT('SELECT C.*, ', @sql, ' FROM Cars C INNER JOIN Stock S ON C.Id = S.Car_Id  INNER JOIN Dice D ON S.DICE_ID = D.ID GROUP BY C.ID');

PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Combine multiple rows from multiple tables in MySQL

From Dev

MySQL join multiple columns from multiple tables

From Dev

MySQL combine rows from multiple tables into on row

From Dev

DELETE rows from multiple tables with JOIN in Mysql

From Dev

MYSQL Select from tables based on multiple rows

From Dev

Sum columns from multiple tables in MySQL

From Dev

MySQL Displaying columns from multiple tables

From Dev

MySql rows in to columns (but dynamic rows)

From Dev

SQL Select rows and delete/update from dynamic multiple tables

From Dev

Delete multiple rows from multiple tables in mysql using single value

From Dev

Extract rows from multiple MySQL tables based on date

From Dev

MySQL count rows from multiple tables using join

From Dev

Mysql select rows from multiple tables in specific order

From Dev

Transpose rows into columns when rows are dynamic -MYSQL

From Dev

Mysql : how i can get 3 columns from multiple tables?

From Dev

Select prefixed columns from multiple tables in Java with MySQL

From Dev

join using multiple columns from different tables in mysql

From Dev

arrange columns of multiple tables mysql

From Dev

From Multiple rows into multiple columns

From Dev

Querying rows from multiple tables

From Dev

Converting Multiple rows into columns in MySQL

From Dev

MySQL get multiple rows into columns

From Dev

SUM of counts on dynamic tables as columns MYSQL

From Dev

MySQL delete multiple rows in multiple tables

From Dev

mysql: combining columns from for tables

From Dev

Convert dynamic text data rows into multiple columns

From Dev

show dates in rows as dynamic columns in MySQL

From Dev

MySQL PivotTable - How to convert dynamic columns to rows?

From Dev

Postgresql delete multiple rows from multiple tables

Related Related

  1. 1

    Combine multiple rows from multiple tables in MySQL

  2. 2

    MySQL join multiple columns from multiple tables

  3. 3

    MySQL combine rows from multiple tables into on row

  4. 4

    DELETE rows from multiple tables with JOIN in Mysql

  5. 5

    MYSQL Select from tables based on multiple rows

  6. 6

    Sum columns from multiple tables in MySQL

  7. 7

    MySQL Displaying columns from multiple tables

  8. 8

    MySql rows in to columns (but dynamic rows)

  9. 9

    SQL Select rows and delete/update from dynamic multiple tables

  10. 10

    Delete multiple rows from multiple tables in mysql using single value

  11. 11

    Extract rows from multiple MySQL tables based on date

  12. 12

    MySQL count rows from multiple tables using join

  13. 13

    Mysql select rows from multiple tables in specific order

  14. 14

    Transpose rows into columns when rows are dynamic -MYSQL

  15. 15

    Mysql : how i can get 3 columns from multiple tables?

  16. 16

    Select prefixed columns from multiple tables in Java with MySQL

  17. 17

    join using multiple columns from different tables in mysql

  18. 18

    arrange columns of multiple tables mysql

  19. 19

    From Multiple rows into multiple columns

  20. 20

    Querying rows from multiple tables

  21. 21

    Converting Multiple rows into columns in MySQL

  22. 22

    MySQL get multiple rows into columns

  23. 23

    SUM of counts on dynamic tables as columns MYSQL

  24. 24

    MySQL delete multiple rows in multiple tables

  25. 25

    mysql: combining columns from for tables

  26. 26

    Convert dynamic text data rows into multiple columns

  27. 27

    show dates in rows as dynamic columns in MySQL

  28. 28

    MySQL PivotTable - How to convert dynamic columns to rows?

  29. 29

    Postgresql delete multiple rows from multiple tables

HotTag

Archive