我只是尝试创建一个sql查询,但没有得到

myzjeezk  于 2021-07-26  发布在  Java
关注(0)|答案(3)|浏览(241)


我有两张table。左边的table是垃圾桶,右边的table是垃圾桶。“入球id”是指他们击球的顺序,“出球id”是指他们出局的顺序。下面的报告1显示了我想要的答案,也就是说,那些建立了合作关系的人。我不知道如何编写查询来检索报表1中的数据。这就是我想要的答案。如何为此编写查询?感谢您的帮助!

sg3maiej

sg3maiej1#

这是一个多步骤的方法。表1是指左侧表,即击球id表,表2是指右侧表,即数据中的out id表。new.table1、new.table2、new.table3、new.table4是临时表。根据您的数据更正列名。希望你能得到你的结果。如果发现任何问题,请发表评论。

Create temporary table new.table1
select OUT.ID, IN.ID, Name, (IN.ID - OUT.ID) AS DIFF from table2;

Create temporary table new.table2
select OUT.ID, IN.ID, Name, (OUT.ID + 1) AS NEW.ID from new.table1 where DIFF <= 0;

Create temporary table new.table3
select OUT.ID, IN.ID, Name, DIFF AS NEW.ID from new.table1 where DIFF > 0;

Create temporary table new.table4
select OUT.ID, IN.ID, Name, NEW.ID from new.table2
UNION ALL
select OUT.ID, IN.ID, Name, NEW.ID from new.table3;

---Final Output

select A.Name, B.Name AS Name2 from new.table4 A, table1 B where A.NEW.ID = B.IN.ID;
83qze16e

83qze16e2#

select 
  s1.names,
  s2.names 
from 
  IN_TABLE,
  OUT_TABLE s1,s2 
where 
  s1.IN_ID = s2.IN_ID;

(其中s1和s2是别名)
或者

select 
  left_table.name, 
  right_table.name as Report1 
from 
  left_table,
  right_table 
where 
  left_table.IN_ID = right_table.IN_ID

请尝试让我们知道这是否解决了您的问题。另外,请参见https://www.youtube.com/watch?v=ktvyhentvn8 更多的知识。

yqhsw0fo

yqhsw0fo3#

您需要一个联接或子查询。让我们看一看连接

Select 
  leftTable.Name, 
  rightTable.Name 
from 
  leftTable 
  join rightTable 
    on leftTable.IN_ID = rightTable.In_ID

编辑:从左到左表格和从右到右表格

相关问题