如何从2个表中检索不常见的值?

时间:2021-06-29 13:20:16

I have the 2 following tables t1, t2 with values,

我有以下2个表t1,t2的值,

t1        t2
1         4
2         2
3         3

Now I want to output

现在我要输出

1 
4

How can I get this output in select query ?

如何在选择查询中获得此输出?

3 个解决方案

#1


0  

Another way. Just COUNT them.

其他方式。只计算他们。

This works if the values are unique per table

如果每个表的值是唯一的,则此方法有效

SELECT
    CombinedValue
FROM
    (
    SELECT t1 AS CombinedValue FROM t1
    UNION ALL
    SELECT t2 FROM t2
    ) foo
GROUP BY
    CombinedValue
HAVING
    COUNT(*) = 1

If not unique per table

如果每个表不唯一

SELECT
    CombinedValue
FROM
    (
    SELECT DISTINCT t1 AS CombinedValue FROM t1
    UNION ALL
    SELECT DISTINCT t2 FROM t2
    ) foo
GROUP BY
    CombinedValue
HAVING
    COUNT(*) = 1

#2


5  

This will get you each item from t1 that is not present in t2, and each item in t2 that is not present in t1:

这将从t1获取t2中不存在的每个项目,以及t2中t1中不存在的每个项目:

select t1.id from t1
left join t2 on t2.id = t1.id
where t2.id is null

union all

select t2.id from t2
left join t1 on t1.id = t2.id
where t1.id is null

(I have assumed that the field name in each table is named id just for the sake of being able to write a query against the tables.)

(我假设每个表中的字段名称只是为了能够针对表编写查询而命名为id。)

Another way would be:

另一种方式是:

select coalesce(t1.id, t2.id)
from t1
full outer join t2 on t2.id = t1.id
where t1.id is null or t2.id is null

#3


0  

you can use Joins in MySql to proceed and to obtain result.

您可以在MySql中使用Joins继续并获取结果。

this will help you http://www.techrepublic.com/article/sql-basics-query-multiple-tables/1050307

这将对你有所帮助http://www.techrepublic.com/article/sql-basics-query-multiple-tables/1050307

#1


0  

Another way. Just COUNT them.

其他方式。只计算他们。

This works if the values are unique per table

如果每个表的值是唯一的,则此方法有效

SELECT
    CombinedValue
FROM
    (
    SELECT t1 AS CombinedValue FROM t1
    UNION ALL
    SELECT t2 FROM t2
    ) foo
GROUP BY
    CombinedValue
HAVING
    COUNT(*) = 1

If not unique per table

如果每个表不唯一

SELECT
    CombinedValue
FROM
    (
    SELECT DISTINCT t1 AS CombinedValue FROM t1
    UNION ALL
    SELECT DISTINCT t2 FROM t2
    ) foo
GROUP BY
    CombinedValue
HAVING
    COUNT(*) = 1

#2


5  

This will get you each item from t1 that is not present in t2, and each item in t2 that is not present in t1:

这将从t1获取t2中不存在的每个项目,以及t2中t1中不存在的每个项目:

select t1.id from t1
left join t2 on t2.id = t1.id
where t2.id is null

union all

select t2.id from t2
left join t1 on t1.id = t2.id
where t1.id is null

(I have assumed that the field name in each table is named id just for the sake of being able to write a query against the tables.)

(我假设每个表中的字段名称只是为了能够针对表编写查询而命名为id。)

Another way would be:

另一种方式是:

select coalesce(t1.id, t2.id)
from t1
full outer join t2 on t2.id = t1.id
where t1.id is null or t2.id is null

#3


0  

you can use Joins in MySql to proceed and to obtain result.

您可以在MySql中使用Joins继续并获取结果。

this will help you http://www.techrepublic.com/article/sql-basics-query-multiple-tables/1050307

这将对你有所帮助http://www.techrepublic.com/article/sql-basics-query-multiple-tables/1050307