Mysql - Merge multiple columns into one while preserving each value

aroth

Is there a simple way in Mysql to convert a multi-column resultset into a single-column resultset where each row in the single column contains a single value from each cell in the multi-column resultset?

For instance, say I have a table like:

    id    |   fk1   |   fk2   |   fk3   
    1          2         3         4
    5          6         7         8

Ideally I'd like to be able to run a query along the lines of:

SELECT <some_function>(fk1, fk2, fk3) AS value FROM myTable;

...and then get an output like:

  value  
    2
    3
    4
    6
    7
    8

Is there a straightforward way of doing so, or is the only real option to walk the multi-column resultset in code and extract the values into the format that I want?

The end goal is to be able to use the first query as a subquery in a context where the input can only be a single column of values, like:

SELECT * FROM myOtherTable WHERE id IN 
    (SELECT <some_function>(fk1, fk2, fk3) AS value FROM myTable);

Thus a pure SQL solution is preferable, if one exists.

Gordon Linoff

The simplest way is to use union all:

select fk1 from mytable union all
select fk2 from mytable union all
select fk3 from mytable;

There are other methods, but this is simplest.

For your particular query, you can use exists and or or in:

SELECT mo.*
FROM myOtherTable mo
WHERE EXISTS (SELECT 1
              FROM MyTable m
              WHERE mo.id IN (m.fk1, m.fk2, m.fk3)
             );

EDIT:

For performance, but an index on each fk column and use multiple conditions in the where:

SELECT mo.*
FROM myOtherTable mo
WHERE EXISTS (SELECT 1 FROM MyTable m WHERE mo.id = m.fk1) OR
      EXISTS (SELECT 1 FROM MyTable m WHERE mo.id = m.fk2) OR
      EXISTS (SELECT 1 FROM MyTable m WHERE mo.id = m.fk3);

This will do index lookups, which should be much faster than the original form.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL merge rows with same ID while preserving timestamps

From Dev

Merge values in multiple columns into one

From Dev

Merge multiple DataFrame columns into one

From Dev

Merging multiple binary columns in R into one column preserving position

From Dev

Merge multiple columns into one column with multiple rows

From Dev

How to merge multiple columns values into one column?

From Dev

Merge data from multiple columns into one column

From Dev

Merge multiple google spreadsheet columns into one column

From Dev

Merge multiple columns data into one pandas

From Dev

How to merge each two columns into one and the number of columns into half

From Dev

MySQL select multiple columns into one

From Dev

Insert multiple columns into one MySQL

From Dev

Merge columns into row by one colunm value

From Dev

mysql group by value in multiple columns

From Dev

Merge objects while preserving unique attributes

From Dev

Extract multiple lines from large text file with sed while preserving each trailing newline (Bash Script)

From Dev

Merge multiple rows into one with total - mysql

From Dev

How to Merge Multiple Columns in to Two Columns based on Column 1 Value?

From Dev

How to create a DataFrame while preserving order of the columns?

From Dev

map over tibble columns while preserving groups

From Dev

Merge multiple files preserving the original sequence in unix

From Dev

Concatenating multiple columns into one while copying values of other columns

From Dev

Hibernate set index on multiple columns while one of the columns is the ID

From Dev

mysql/php - merge multiple arrays from mysql output in while statement

From Dev

ManyToOne Multiple Join Columns, One is Default Value

From Dev

Sum and grouping multiple columns into one value in linq

From Dev

Format multiple columns depending on the value of one

From Dev

Transpose multiple columns into one row mysql

From Dev

MySQL Count multiple columns in one query

Related Related

  1. 1

    MySQL merge rows with same ID while preserving timestamps

  2. 2

    Merge values in multiple columns into one

  3. 3

    Merge multiple DataFrame columns into one

  4. 4

    Merging multiple binary columns in R into one column preserving position

  5. 5

    Merge multiple columns into one column with multiple rows

  6. 6

    How to merge multiple columns values into one column?

  7. 7

    Merge data from multiple columns into one column

  8. 8

    Merge multiple google spreadsheet columns into one column

  9. 9

    Merge multiple columns data into one pandas

  10. 10

    How to merge each two columns into one and the number of columns into half

  11. 11

    MySQL select multiple columns into one

  12. 12

    Insert multiple columns into one MySQL

  13. 13

    Merge columns into row by one colunm value

  14. 14

    mysql group by value in multiple columns

  15. 15

    Merge objects while preserving unique attributes

  16. 16

    Extract multiple lines from large text file with sed while preserving each trailing newline (Bash Script)

  17. 17

    Merge multiple rows into one with total - mysql

  18. 18

    How to Merge Multiple Columns in to Two Columns based on Column 1 Value?

  19. 19

    How to create a DataFrame while preserving order of the columns?

  20. 20

    map over tibble columns while preserving groups

  21. 21

    Merge multiple files preserving the original sequence in unix

  22. 22

    Concatenating multiple columns into one while copying values of other columns

  23. 23

    Hibernate set index on multiple columns while one of the columns is the ID

  24. 24

    mysql/php - merge multiple arrays from mysql output in while statement

  25. 25

    ManyToOne Multiple Join Columns, One is Default Value

  26. 26

    Sum and grouping multiple columns into one value in linq

  27. 27

    Format multiple columns depending on the value of one

  28. 28

    Transpose multiple columns into one row mysql

  29. 29

    MySQL Count multiple columns in one query

HotTag

Archive