-1

enter image description here

Hello everyone,

1/ I want to do a sql query that transform the result from row to column like it's shown in the picture. Is it possible ?

2/ How can I update the second table with the first one ?

Julie Tarnaud
  • 15
  • 1
  • 5
  • Does this answer your question? [MySQL - turn table into different table](https://stackoverflow.com/questions/15184381/mysql-turn-table-into-different-table) – nbk Oct 08 '20 at 15:10
  • if you have a static column count see the duplicate, if the count is dynamic you have to use INFORMATION SCHEMA.columns for that and program it – nbk Oct 08 '20 at 15:11
  • Just rotate your monitor – Strawberry Oct 08 '20 at 18:23

2 Answers2

0

You could use in the query

select 'EH545WL' as name, EH545WL as value from car union select 'FJ281TJ' as name, FJ281TJ as value from car;

If you use this query, you could get the result you want. Thank you

Great Coder
  • 397
  • 3
  • 14
0

There are several ways that you can transform this data.

Create Table:

CREATE TABLE yourTable([EH545WL] varchar(10), [FJ281TJ] varchar(10));

INSERT INTO yourTable
VALUES
    (0,0);

Unpivot table:

SELECT ID,Value
FROM yourTable
UNPIVOT (value for ID in ([EH545WL],[FJ281TJ])) up
Priyanka2304
  • 200
  • 3
  • 16
  • Okay thank you very much ! One more question is that how can I update the second table with the first one ? – Julie Tarnaud Oct 09 '20 at 13:21
  • @JulieTarnaud One thing you can do is after you get the desired output, drop the first table. Also please upvote/accept the answer if it works for you. – Priyanka2304 Oct 09 '20 at 17:22
  • I don't see your point. Because I need first table to update the second but I don't know how to realize the update – Julie Tarnaud Oct 09 '20 at 19:01
  • @JulieTarnaud Please see the updated sql code that lets you create table and update the same table in unpivot mode. – Priyanka2304 Oct 09 '20 at 21:42