根据table1中的匹配项和table2中的不匹配项将数据插入table3

时间:2016-12-20 12:00:53

标签: sql oracle

想要制作简单的Lost and Found数据库。我有table2(在系统中添加)" costumer"进来并定义" ztracena _ / _ nalezea" 0表示丢失,1表示发现。 然后他更多地指出table1中的东西,如重量,颜色等...... 现在我想用这两个数据填充table3(匹配)。我需要在table2中匹配,但我需要这些东西是一个丢失0和一个找到1(table2)。 真的希望我解释得足够简单。感谢帮助! enter image description here

1 个答案:

答案 0 :(得分:0)

如果您能提供样本数据和所需结果,那将会容易得多。我认为这样做你想要的:

with t1 as (
  select 1 as flag, 1 as id from dual union all
  select 1 as flag, 2 as id from dual union all
  select 0 as flag, 3 as id from dual union all
  select 0 as flag, 4 as id from dual
), t2 as (
  select 1 as id, 'yellow' as color, 'hat' as category from dual union all
  select 2 as id, 'red' as color, 'box' as category from dual union all
  select 3 as id, 'yellow' as color, 'hat' as category from dual union all
  select 4 as id, 'red' as color, 'hat' as category from dual
)
select tflag0.id || ' matches ' || tflag1.id from 
  (select t1.id, t2.color, t2.category from t1  inner join t2 on (t1.id = t2.id) where flag = 0) tflag0 --things lost
  inner join 
  (select t1.id, t2.color, t2.category from t1  inner join t2 on (t1.id = t2.id) where flag = 1) tflag1 --things found
  on (tflag0.color = tflag1.color and tflag0.category = tflag1.category);

第一部分将t1定义为您的表格,我们在t2中放置了意味着找到/丢失的内容,我们对事物进行了描述。查询为id t1提供了.tiles-menu{ width:1024px; margin:auto; } .tiles-menu .submenu-menu .views-row{ float:left; } .tilesImage , .tilesTitle{ margin:auto; } .innerPageTilesMenu{ padding:4%; }个相同的描述,一个人找到了它,另一个人丢失了它。现在请尝试使解决方案适应您的模型。