简体   繁体   中英

Match column_name to Column data

I am trying to work with a two (already) generated tables. In one table 1 there is a column named column_code In table 2 the columns are named like the data in column_code

Per example Table 1:

meta_data
----------------------------------
| pid         | 1                |
| question    | favorite website |
| column_code | AA123ZZ          |
---------------------------------- 

where column_code has a content named: 'AA123ZZ' and question is called: what is your favorite website

Table 2:

content
-----------------------------------
| id          | 4                 |
| submit_date | 14-01-2013        |
| AA123ZZ     | stackoverflow.com |
-----------------------------------

The content of this table_name AA123ZZ is for instance " stackoverflow.com "

Now what I want is a query where the result will be something like:

id: = 1
what is your favorite website: stackoverflow.com

So actually AA123ZZ stands for this question, I have no other way of matching these together, how can I do this?

I am sorry I cannot make this any clearer. I got these two tables and indeed they seem to be missing something.

So to try make this a bit clearer.

In the content table the table name AA123ZZ is 'code' for the question.

The maker of the database should just have placed the question as column name in stead of this code AA123ZZ . Now I need to match this code to what is inside meta_data .

So in meta_data in the question_column you can find: "favorite website" and in the column_code you will find AA123ZZ .

in the content table in the AA123ZZ column you will find: stackoverflow.com

As you can see I cannot match these easily on a inner join.

So is there a way to match a column_name from one table to the contents of a column in another table?

You can't join tables where the column name itself is variable. You're going to need to fix the schema here, perhaps to include a foreign key value to the meta_data table from the content table and from there you can do a simple join on the two tables.

Read more on foreign keys

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM