Mysql - Select tuple values using dynamic column names

Asked

Viewed 90 times

0

It is a filter where the user selects the days of the week and sends to the bank to filter but I am not able to perform the filtering. The table has as column the days of the week with several records. For example has company that works from Monday to Friday so register on the line with id 1 in the column according to the time 8:00-18:00 Tuesdays 8:00-18:00 Tuesdays 8:00-18:00 Tuesdays 8:00-18:00 Tuesdays Closed Closed Closed Closed Closed Closed Sun and the company receives the id of that line as FK. There are several lines with different Sun Hours to Ab. Now the filter, if it passes second then I want to select all the rows where the second column is different from closed. I did so:

select * from funcionamento where 
    (select column_name from INFORMATION_SCHEMA.columns where 
        table_name = 'funcionamento' AND column_name like '%segunda') 
    != 'Fechado';

However it did not work, he finds the second column but not the fields with value other than closed.

inserir a descrição da imagem aqui

  • try to post image of how your bank is modeled, could not understand very well, but it seems that you are missing use Internet

  • I thought about using Inner Join but I couldn’t think of a way

  • In my idea when changing the subquery by the column name it would filter all values other than closed but that’s not what happens

  • But if column_name like '%second' as could be != 'Closed'?

  • Running the subquery and taking the column name, when replacing the subquery would be select * from Where function_string_second != 'Closed'; and then it would filter. At least it should be.

  • Search by Prepared statement. https://dev.mysql.com/doc/refman/8.0/en/sql-syntax-prepared-statements.html

  • by the image of your table is not just pick up select * from working Where function_string_second != 'Closed' ?

  • It is not, because as it is a filter and the user chooses the days of the week I will not know if it will be Monday, Tuesday, Wednesday... We don’t know which column to check is different from closed, only when he chooses

  • I’ve tried concatenating the checkbox value with the column name but it doesn’t solve either. For example value='second' and in query= '...CONCAT('function_string_',:day)... but query does not accept

  • I resolved with the use of a view. Thank you all

  • I would normalize this model.

  • And how would you normalize? For me you are already in 1,2 and 3 FN

Show 7 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.