'Query to select values from the same column as given values, but different rows

I have a table for a school's time table, as follows, where p1..p4 are 'periods' The values of periods p1,p2,p3,p4 in the first row contain 'timings' of subjects s1,s2,s3,... on certain days. It is required to return a list of all the timings of a given list of subjects on a particular day.

assume all fields are strings

---------t_time--------
    day  p1  p2  p3  p4
0   null 4   5   6   7
1   mon  s1  s2  s3  s4
2   tue  s2  s3  s4  s5
3   wed  s3  s4  s5  s1
4   mon  s2  s4  s3  s1

For example, for day = ['mon','wed'] sub = ['s1','s3'], the output would be as such:

  day sub time
0 mon s1  4
1 mon s3  6
3 wed s3  4
4 wed s1  7
5 mon s1  7

Until now I am using a very inefficient search loop in php|mysqli and want to change it.

I thought separating the 0th row as its own table which would make more sense, but apparently its a constraint i have got to work with

---------t_time--------
    day  p1  p2  p3  p4
0   mon  a   b   c   d
1   tue  b   e   a   b
2   wed  p   q   d   e
3   mon  m   n   o   p
---------p_time--------
    period  time
0   p1      4
1   p2      5
2   p3      6
3   p4      7

But in this case I would somehow have to query the column_names of columns that contained particular values in a row, which I cannot understand.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source