T1表
Pk1 name
1 jh
2 dr
3 egb
4 jvv
T2表
Pk2 Fk2 score
1 1 4
2 1 5
3 1 6
4 2 7
5 2 1
6 3 1
7 4 6
T2表的FK2指向T1表的主键PK1
要求查询出结果 :T1表的:pk1, name 以及相应score的总和sum>6
5 个解决方案
#1
sum > 6 是什么意思?
#2
CREATE TABLE T_T1 (
PK1 NUMBER(8) NOT NULL,
NAME VARCHAR2(20) NOT NULL,
CONSTRAINT PK_T1 PRIMARY KEY (PK1)
);
CREATE TABLE T_T2 (
PK2 NUMBER(8) NOT NULL,
FK2 NUMBER(8) NOT NULL,
SCORE NUMBER(8) NOT NULL,
CONSTRAINT PK_T2 PRIMARY KEY (PK2)
);
INSERT INTO t_t1 (pk1, name) VALUES (1, 'jh');
INSERT INTO t_t1 (pk1, name) VALUES (2, 'dr');
INSERT INTO t_t1 (pk1, name) VALUES (3, 'egb');
INSERT INTO t_t1 (pk1, name) VALUES (4, 'jvv');
INSERT INTO t_t2 (pk2, fk2, score) VALUES (1, 1, 4);
INSERT INTO t_t2 (pk2, fk2, score) VALUES (2, 1, 5);
INSERT INTO t_t2 (pk2, fk2, score) VALUES (3, 1, 6);
INSERT INTO t_t2 (pk2, fk2, score) VALUES (4, 2, 7);
INSERT INTO t_t2 (pk2, fk2, score) VALUES (5, 2, 1);
INSERT INTO t_t2 (pk2, fk2, score) VALUES (6, 3, 1);
INSERT INTO t_t2 (pk2, fk2, score) VALUES (7, 4, 6);
commit;
SELECT a.pk1, a.name, sum(b.score)
FROM t_t1 a
LEFT JOIN t_t2 b ON a.pk1 = b.fk2
GROUP BY a.pk1, a.name
HAVING sum(b.score) > 6
#3
SQL> SELECT a.pk1, a.name, sum(b.score)
2 FROM t_t1 a
3 LEFT JOIN t_t2 b ON a.pk1 = b.fk2
4 GROUP BY a.pk1, a.name
5 ORDER BY a.pk1;
PK1 NAME SUM(B.SCORE)
--------- -------------------- ------------
1 jh 15
2 dr 8
3 egb 1
4 jvv 6
SQL>
SQL> SELECT a.pk1, a.name, sum(b.score)
2 FROM t_t1 a
3 LEFT JOIN t_t2 b ON a.pk1 = b.fk2
4 GROUP BY a.pk1, a.name
5 HAVING sum(b.score) > 6
6 ORDER BY a.pk1;
PK1 NAME SUM(B.SCORE)
--------- -------------------- ------------
1 jh 15
2 dr 8
#4
在神州泰岳做的笔试题目
答案做的和火龙果一样
但是在给人家解释的时候没有解释清楚
最后我说应该做错了吧
人家就没有问了
哎呀
郁闷啊
答案做的和火龙果一样
但是在给人家解释的时候没有解释清楚
最后我说应该做错了吧
人家就没有问了
哎呀
郁闷啊
#5
但还是很欣赏面我的那个经理,很随和,也很有N人的风度...
#1
sum > 6 是什么意思?
#2
CREATE TABLE T_T1 (
PK1 NUMBER(8) NOT NULL,
NAME VARCHAR2(20) NOT NULL,
CONSTRAINT PK_T1 PRIMARY KEY (PK1)
);
CREATE TABLE T_T2 (
PK2 NUMBER(8) NOT NULL,
FK2 NUMBER(8) NOT NULL,
SCORE NUMBER(8) NOT NULL,
CONSTRAINT PK_T2 PRIMARY KEY (PK2)
);
INSERT INTO t_t1 (pk1, name) VALUES (1, 'jh');
INSERT INTO t_t1 (pk1, name) VALUES (2, 'dr');
INSERT INTO t_t1 (pk1, name) VALUES (3, 'egb');
INSERT INTO t_t1 (pk1, name) VALUES (4, 'jvv');
INSERT INTO t_t2 (pk2, fk2, score) VALUES (1, 1, 4);
INSERT INTO t_t2 (pk2, fk2, score) VALUES (2, 1, 5);
INSERT INTO t_t2 (pk2, fk2, score) VALUES (3, 1, 6);
INSERT INTO t_t2 (pk2, fk2, score) VALUES (4, 2, 7);
INSERT INTO t_t2 (pk2, fk2, score) VALUES (5, 2, 1);
INSERT INTO t_t2 (pk2, fk2, score) VALUES (6, 3, 1);
INSERT INTO t_t2 (pk2, fk2, score) VALUES (7, 4, 6);
commit;
SELECT a.pk1, a.name, sum(b.score)
FROM t_t1 a
LEFT JOIN t_t2 b ON a.pk1 = b.fk2
GROUP BY a.pk1, a.name
HAVING sum(b.score) > 6
#3
SQL> SELECT a.pk1, a.name, sum(b.score)
2 FROM t_t1 a
3 LEFT JOIN t_t2 b ON a.pk1 = b.fk2
4 GROUP BY a.pk1, a.name
5 ORDER BY a.pk1;
PK1 NAME SUM(B.SCORE)
--------- -------------------- ------------
1 jh 15
2 dr 8
3 egb 1
4 jvv 6
SQL>
SQL> SELECT a.pk1, a.name, sum(b.score)
2 FROM t_t1 a
3 LEFT JOIN t_t2 b ON a.pk1 = b.fk2
4 GROUP BY a.pk1, a.name
5 HAVING sum(b.score) > 6
6 ORDER BY a.pk1;
PK1 NAME SUM(B.SCORE)
--------- -------------------- ------------
1 jh 15
2 dr 8
#4
在神州泰岳做的笔试题目
答案做的和火龙果一样
但是在给人家解释的时候没有解释清楚
最后我说应该做错了吧
人家就没有问了
哎呀
郁闷啊
答案做的和火龙果一样
但是在给人家解释的时候没有解释清楚
最后我说应该做错了吧
人家就没有问了
哎呀
郁闷啊
#5
但还是很欣赏面我的那个经理,很随和,也很有N人的风度...