English 中文(简体)
两个表的合并——两个表的交叉点
原标题:union of two table - intersection of two table
  • 时间:2012-01-15 14:00:11
  •  标签:
  • sql
  • sqlite

我有以下表格:

 A   B                       A   B
 _____                       _____
 1   t                       7   a
 2   r                       5   d
 3   e                       3   e
 4   f                       9   a
 5   d                       10  c
 6   s                       11  a
 7   a

产出应为:

 A   B                     
 _____                     
 1   t                      
 2   r                     

 4   f

 6   s

 9   a
 10  c
 11  a

In other words I want really different thing. I can only tell with this figure, take a look at. I want (A union B). How can I do that ?

最佳回答

这样做。 它从两个表格中汇集了所有记录,然后显示了一度存在的所有记录。

SELECT
  A, B
FROM
  (SELECT A, B FROM TABLE1
   UNION ALL
   SELECT A, B FROM TABLE2)
  AS COMBINED
GROUP BY
  A, B
HAVING
    COUNT(*) = 1
ORDER BY A;
问题回答
SELECT f.A, f.B
FROM firstTable f
LEFT JOIN secondTable s ON (f.A = s.A)
WHERE (s.A IS NULL)
UNION
SELECT s.A, s.B
FROM firstTable f
RIGHT JOIN secondTable s ON (f.A = s.A)
WHERE (f.A IS NULL)
SELECT iResult.* 
FROM
    (SELECT A, B
    FROM tableA
    WHERE A NOT IN
        (SELECT Distinct A FROM tableB)
    UNION
    SELECT A, B
    FROM tableB
    WHERE A NOT IN
        (SELECT Distinct A FROM tableB)) as iResult
SELECT A, B
FROM Table1

UNION

SELECT A, B
FROM Table2

EXCEPT

SELECT t1.A, t1.B
FROM Table1 t1
  INNER JOIN Table2 t2 ON t1.A = t2.A AND t1.B = t2.B

<><>Edit>/strong> 删除了先前的“溶解”一词,因为我认识到这与加里提出的解决办法相同。

(select a, b from table_1 minus 
 select a, b from table_2)       union
(select a, b from table_2 minus 
 select a, b from table_1);

这似乎与甲骨文的后续数据有关:

create table table_1 (
  a number,
  b varchar(2)
);

create table table_2 (
  a number,
  b varchar(2)
);


insert into table_1 values  (1  , t );
insert into table_1 values  (2  , r );
insert into table_1 values  (3  , e );
insert into table_1 values  (4  , f );
insert into table_1 values  (5  , d );
insert into table_1 values  (6  , s );
insert into table_1 values  (7  , a );

insert into table_2 values  (7  , a );
insert into table_2 values  (5  , d );
insert into table_2 values  (3  , e );
insert into table_2 values  (9  , a );
insert into table_2 values  (10 , c );
insert into table_2 values  (11 , a );

简单

SELECT a, b FROM table1 UNION SELECT a, b FROM table2;




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签