ash
ash

Reputation: 801

Mysql pivot like functionality in code

I am tackling a problem where I have to convert Table 1 to Table 2 using mysql

Table 1

Table 2

Table 2 structure is only define. How can I go about this? I have tried using union statement but am unable to convert Has on Monday to Monday as the column value

Upvotes: 0

Views: 22

Answers (1)

fthiella
fthiella

Reputation: 49089

You should write your query as this:

SELECT student, class, 'Monday' AS weekday
FROM table_1
WHERE has_on_monday='T'
UNION ALL
SELECT student, class, 'Tuesday' AS weekday
FROM table_1
WHERE has_on_tuesday='T'
UNION ALL
....

if you want to insert the result to table_2, use a INSERT query:

INSERT INTO table_2 (student, class, weekday)
SELECT ...the select query above...

Upvotes: 2

Related Questions