In answer to your first question, you should learn how to make JOIN in SQL. Joining is a fundamental SQL operation. Understanding how to make a loop in other languages ββis important.
SELECT DISTINCT users.user_name FROM users JOIN entries USING (user_id) WHERE entries.header = 'foo';
As for the second question, no, you cannot dynamically change the names of tables or columns in a single statement.
However, you can write code in your application to create an SQL statement as a string based on a search for column names and table names. Then execute the resulting string as a new SQL query.
source share