简体   繁体   中英

MySQL column references different table names

I have multiple tables, each holds results from very different types of tests: test_a, test_b, test_c etc. One 'person' can do multiple tests of the same or different types of test. current erd

At present I reference each table holding test results using a seperate indexed columns in table test_ref. Meaning I have to add a new column every time I add a new test table / type.

Is it possible to change this? Ideally test_ref should only ever point to one unique test from any of the available test tables.

So my question is probabaly how do i reference multiple tables from one column?

I hope this is clear I'm new database design, I have looked for examples of this but I must be missing something becasue I can't find other examples. Many thanks in advance.

There you need to put into one column test_id values/reference from test_a, test_b, test_c, and add column with test_type (where you have option "A","B", ...).

SELECT 
    *
FROM 
    test_ref tr
INNER JOIN
    test_a tt on tr.test_id = tt.id AND tr.test_type = "A";

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