表A A_1 A_2
QW 1
QE 2
QR 3
QT 4
表B B_1 B_2
a 1
b 2
c 3
d 4
表C C_1 C_2
a 2
b 1
c 3
d 3
现在我想得到的结果是
B_2 A_1 C_2 A_1
1 QW 2 QE
2 QE 1 QW
4 QT 3 QR
满足的条件就是B1=C1,B2<>C2。
SELECT 怎么写呢
4 个解决方案
#1
PL/SQL??oracle就去相应版块吧
#2
CREATE TABLE 表A
(
A_1 VARCHAR(10),
A_2 INT
)
INSERT INTO 表A
SELECT 'QW', 1 UNION
SELECT 'QE', 2 UNION
SELECT 'QR', 3 UNION
SELECT 'QT', 4
CREATE TABLE 表B
(
B_1 VARCHAR(10),
B_2 INT
)
INSERT INTO 表B
SELECT 'a', 1 UNION
SELECT 'b', 2 UNION
SELECT 'c', 3 UNION
SELECT 'd', 4
CREATE TABLE 表C
(
C_1 VARCHAR(10),
C_2 INT
)
INSERT INTO 表C
SELECT 'a', 2 UNION
SELECT 'b', 1 UNION
SELECT 'c', 3 UNION
SELECT 'd', 3
SELECT B_2,A.A_1,C_2,B.A_1
FROM 表B,表C,表A AS A,表A AS B
WHERE B_1 = C_1 AND B_2 <> C_2 AND B_2 = A.A_2 AND C_2 = B.A_2
B_2 A_1 C_2 A_1
2 QE 1 QW
4 QT 3 QR
1 QW 2 QE
#3
#4
SELECT B_2,a.A_1,C_2,b.A_1 as A_1
FROM 表B,表C,表A a,表A b
WHERE B_1 = C_1 AND B_2 <> C_2 AND B_2 = a.A_2 AND C_2 = b.A_2
执行结果:
FROM 表B,表C,表A a,表A b
WHERE B_1 = C_1 AND B_2 <> C_2 AND B_2 = a.A_2 AND C_2 = b.A_2
执行结果:
#1
PL/SQL??oracle就去相应版块吧
#2
CREATE TABLE 表A
(
A_1 VARCHAR(10),
A_2 INT
)
INSERT INTO 表A
SELECT 'QW', 1 UNION
SELECT 'QE', 2 UNION
SELECT 'QR', 3 UNION
SELECT 'QT', 4
CREATE TABLE 表B
(
B_1 VARCHAR(10),
B_2 INT
)
INSERT INTO 表B
SELECT 'a', 1 UNION
SELECT 'b', 2 UNION
SELECT 'c', 3 UNION
SELECT 'd', 4
CREATE TABLE 表C
(
C_1 VARCHAR(10),
C_2 INT
)
INSERT INTO 表C
SELECT 'a', 2 UNION
SELECT 'b', 1 UNION
SELECT 'c', 3 UNION
SELECT 'd', 3
SELECT B_2,A.A_1,C_2,B.A_1
FROM 表B,表C,表A AS A,表A AS B
WHERE B_1 = C_1 AND B_2 <> C_2 AND B_2 = A.A_2 AND C_2 = B.A_2
B_2 A_1 C_2 A_1
2 QE 1 QW
4 QT 3 QR
1 QW 2 QE
#3
#4
SELECT B_2,a.A_1,C_2,b.A_1 as A_1
FROM 表B,表C,表A a,表A b
WHERE B_1 = C_1 AND B_2 <> C_2 AND B_2 = a.A_2 AND C_2 = b.A_2
执行结果:
FROM 表B,表C,表A a,表A b
WHERE B_1 = C_1 AND B_2 <> C_2 AND B_2 = a.A_2 AND C_2 = b.A_2
执行结果: