在多个表中搜索相同的值,并获取结果来自的表

问题描述:

对于我的问题的第一部分,我发现了一个相似且已被接受的老问题,但解决方案不起作用->

For the first part of my question, I have found an old question that is similar and has an accepted answer but the solution does not work -> Search multiple tables for the same value

所以被接受的答案是:

SELECT * FROM table_one, table_two WHERE field = 'some_val'

当我尝试遇到此错误时

where子句中的字段"field"不明确

Column 'field' in where clause is ambiguous

我尝试使用以下方法解决该问题:

I have tried to solve that using:

SELECT
    *
FROM
    table_one,
    table_two
WHERE
    table_one.field = 'some_val'
OR table_two.field = 'some_val';

解决了以上错误,但结果是:table_one中没有任何内容,table_two中的所有条目都是

That solves the above error but the result is: nothing from table_one and all entries from table_two

我必须澄清一下,在我的测试中,找到"some_val"的值存在于table_one中,而不存在于table_two中

这是我问题的第一部分.

So this has been for the first part of my question.

第二部分.

假设第一部分有解决方案,问题的第二部分将是:

Assuming there will be the solution for the first part, the second part of the question would be:

如何检查结果从何而来? ...在哪里找到"some_val"? (在table_one中在table_two中或在两个表中)?

How to check where does the result come from? ... Where 'some_val' has been found? (in table_one in table_two or both) ?

使用联合解决您的问题.您可以在

Use union for your problem.You can find more details regarding union in this link

SELECT
    table_one.col_1,
    table_one.col_2,
    'table_one' AS from_table
FROM
    table_one
WHERE
    table_one.field = 'some_val'
UNION
    SELECT
        table_two.col_1,
        table_two.col_2,
        'table_two' AS from_table
    FROM
        table_two
    WHERE
        table_two.field = 'some_val'