Hello is it possible to make an ordered select statement in MYSQL where the user will select the column name to be ordered by. I know that I can make a list of predefined choices for the user but the user has the option to create his own columns for each table.
At first I was thinking to check if the column is inside the results of a query like:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'my_database' AND TABLE_NAME = 'my_table';
or
SHOW COLUMNS FROM my_table;
but I'd like like a cleaner solution for this and I'm not so proficient with mysql. In other words:
SELECT * FROM 'users' WHERE users.id = 'user_id' ORDER BY 'column' IF EXISTS ELSE ORDER BY users.id
something like the above statement, if column doesnt exist order by id like the defulat order.