表1:问题
id | question
----------------
1 | Name
2 | Age
3 | Gender
4 | Position
表2: answer
qid | ans | record
-----------------------
1 | Jay | 1
2 | 24 | 1
3 | M | 1
2 | 23 | 2
我想提出一个连接查询,产生下表:
record | question | ans
-----------------------------
1 | Name | Jay
1 | Age | 24
1 | Gender | M
1 | Position | null
2 | Name | null
2 | Age | 23
2 | Gender | null
2 | Position | null
我能想到的最接近的是这种联接:
select a.record, q.question, a.ans
from
question q left outer join answer a
on q.id = a.qid order by a.record,q.id;
但是,此查询只会产生此问题,但我希望所有问题都显示两次
record | question | ans
-----------------------------
1 | Name | Jay
1 | Age | 24
1 | Gender | M
1 | Position | null
2 | Age | 23
答案 0 :(得分:2)
您需要交叉连接以生成所需的所有组合,并与左连接配对以检索答案,如:
index.html 1.61 KiB [emitted]
static/js/bundle.js.map 32.1 KiB runtime~main [emitted] static/js/main.chunk.js 15.5 KiB main [emitted] main
static/js/main.chunk.js.map 12.1 KiB main [emitted] main
Entrypoint main = static/js/bundle.js ... blah blah
ERROR in /foo/src/index.tsx
ERROR in /foo/src/index.tsx(14,20):
TS2304: Cannot find name 'nonExistentVar'.
ERROR in /foo/src/index.tsx
ERROR in /foo/src/index.tsx(15,20):
TS2304: Cannot find name 'nonExistentVar2'.
Child HtmlWebpackCompiler:
1 asset
Entrypoint HtmlWebpackPlugin_0 = __child-HtmlWebpackPlugin_0
[./node_modules/html-webpack-plugin/lib/loader.js!./public/index.html] 1.75 KiB {HtmlWebpackPlugin_0} [built]
[./node_modules/webpack/buildin/global.js] (webpack)/buildin/global.js 475 bytes {HtmlWebpackPlugin_0} [built]
[./node_modules/webpack/buildin/module.js] (webpack)/buildin/module.js 546 bytes {HtmlWebpackPlugin_0} [built]
+ 1 hidden module
error Command failed with exit code 2.
结果:
select
r.record,
q.question,
a.ans
from question q
cross join (select distinct record from answer) r
left join answer a on a.record = r.record and a.qid = q.id
order by r.record, q.id
作为参考,这是我用来验证情况的测试脚本:
record question ans
------ -------- ------
1 Name Jay
1 Age 24
1 Gender M
1 Position <null>
2 Name <null>
2 Age 23
2 Gender <null>
2 Position <null>