【发布时间】:2018-07-29 11:57:40
【问题描述】:
我有下表:
╔═══╦══════════════╦═════════════╗
║ ║id ║name ║
╠═══╬══════════════╬═════════════╣
║ ║ 1 ║a1 ║
║ ║ 1 ║b1 ║
║ ║ 2 ║b2 ║
║ ║ 3 ║c1 ║
║ ║ 2 ║c2 ║
║ ║ 4 ║a2 ║
╚═══╩══════════════╩═════════════╝
我有以下查询,它执行以下操作:
对于输入 (a,b,c),它返回 (aX,bX,cX) 形式的所有可能组合,其中 X 是记录中“a/b/c”之后出现的任何内容。
(a,b) 根据我的表格生成 (a1,b1) , (a1,b2) 。
运行此查询
select t1.id as t1_id, t1.name as t1_name,
t2.id as t2_id, t2.name as t2_name,
t3.id as t3_id, t3.name as t3_name,
from (select * from table where name like 'a%') as t1
cross join (select * from table where name like 'b%') as t2
cross join (select * from table where name like 'c%') as t3
我想修改查询,使其只返回没有相似 ID 的行。
例如,第一行的 t1_id = 1, t2_id = 1 所以有两个相似的 id。这不应该出现在结果中。
【问题讨论】: