我想写下面的查询,以便从jobs表中返回职业和子职业对等于某个用户的职业和子职业对之一的所有职位( 4对中的一个)。如果用户有一对在subprofession = 0,这意味着采取相应的行业全部subprofessions:正确的SQL查询语法和WHERE EXISTS替代方案
是这样的:
select * from jobs j
where
(j.profession, j.subprofession) in
(select (u.profession1, u.subprofession1) from users u where [email protected]),
select (u.profession2, u.subprofession2) from users u where [email protected]),
select (u.profession3, u.subprofession3) from users u where [email protected]),
select (u.profession4, u.subprofession4) from users u where [email protected])
and
j.profession in (select u.profession1, u.profession2, u.profession3, u.profession4 from users u where [email protected]) and (u.subprofession1 = 0 or u.subprofession2 = 0 or u.subprofession3 = 0 or u.subprofession4 = 0))
我知道这个查询语法错误和不该做什么它是应该的,那么使用IN子句或WHERE EXISTS实现上述目的的方式是什么?
jobs: profession, subprofession
1 (100, 200)
2 (100, 300)
3 (100, 400)
4 (100, 500)
5 (200, 300)
6 (400, 500)
7 (400, 100)
8 (400, 600)
9 (200, 200)
10 (600, 700)
11 (100, 100)
12 (500, 300)
13 (200, 200)
users: (prof1, subprof1, prof2, subprof2, prof3, subprof3, prof4, subprof4)
1 (100, 100, 757, 646, 100, 0, 500, 400)
2 (100, 2, 565, 76, 567, 534, 433, 565)
3 (200, 454, 553, 345, 354, 435, 334, 877)
4 (500, 300, 456, 565, 354, 435, 545, 435)
5 (400, 453, 434, 453, 423, 234, 324, 4435)
6 (100, 400, 435, 543, 465, 654, 454, 543)
7 (435, 435, 600, 700, 100, 0, 500, 400)
8 (100, 100, 553, 345, 100, 0, 500, 400)
请求的查询会从工作表中的行返回: 1,2,3,4,11
这些地方的职业和subprofession的值(一对)出现在列其中一个用户prof和subprof对。另外,因为对于用户1有一个条目(prof3 = 100和subprof3 = 0),查询应该返回proession为100的所有订单(针对所有子协议)。
*** SQL ***只是*结构化查询语言* - 许多数据库系统使用的语言,但不是数据库产品......许多东西都是特定于供应商的 - 所以我们真的需要知道什么* *您正在使用的数据库系统**(以及哪个版本)(请相应地更新标签)...... –
查询 – Friederike
有一些示例表和预期结果也是有用的这不清楚:'如果用户有一对,其中subprofession = 0,这意味着采取所有专业为各自职业' – Tim