-2

好的,假设我们有 3 个或更多,比如说“k”个具有相同结构的表:
表 1:

id customer_id
1  35
2  36
3  37

表2:

id customer_id
1  35
2  38
3  39

...
表k:

id customer_id
1  35
2  69
3  52

我如何选择所有表中存在的所有 customer_id(换句话说:交叉点),在这个例子中,如果有 3 个表,结果应该是这样的

id customer_id
1  35

如果有人可以提供帮助,我将不胜感激

4

4 回答 4

0
create table dela (
    id int
);
create table delb (
    id int
);
create table delc (
    id int
);

drop table dela;

insert into dela values (1),(2),(3);
insert into delb values (5),(4),(3);
insert into delc values (5),(1),(3);

select 
    a.id
from
    dela a
        join
    delb b ON a.id = b.id
        join
    delc c ON a.id = c.id;
于 2013-04-05T17:59:47.243 回答
0
SELECT t1.customer_id
FROM `table 1` AS t1
INNER JOIN `table 2` AS t2 ON t1.customer_id = t2.customer_id
-- ...
INNER JOIN `table k` AS tk ON t1.customer_id = tk.customer_id
于 2013-04-05T15:21:57.390 回答
0

尝试这个 :

SELECT 
    customer_id
FROM (
    SELECT
        customer_id
        1 as counter 
    FROM 
        table1

    UNION ALL 

    SELECT
        customer_id
        1 as counter 
    FROM 
        table2

    ....

    SELECT
        customer_id
        1 as counter 
    FROM 
        tablek

) as tmp
GROUP BY
    customer_id
HAVING
    SUM(counter) = k
于 2013-04-05T15:25:38.043 回答
0

这应该有效:

SELECT table1.id, table1.customer_id
FROM table1, table2, ..., tableK
WHERE table1.id=table2.id
AND table1.id=table3.id
...
AND table1.id=tableK.id

为简单起见,您还可以为每个表定义别名:

FROM table1 a, table2 b, ..., tableK k
WHERE a.id=b.id
于 2013-04-05T15:22:35.440 回答