在多个表中搜索相同的值,并获取结果来自的表 [英] Search multiple tables for the same value and get table where result it comes from

查看:68
本文介绍了在多个表中搜索相同的值,并获取结果来自的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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'

这篇关于在多个表中搜索相同的值,并获取结果来自的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆