两个表

时间:2015-07-05 16:37:17

标签: sql oracle join

我是新手SQL用户。

我有两张桌子。 On具有关于线性特征的概述信息,例如要素类型,名称等,table_1。每个线性特征存在于三个空间区域中。第二个表格具有每个线性要素的x,y位置,因为它从一个区域跨越到另一个区域table_2。 x,y在两列中,并且穿过的区域在第三列中。我必须将表彼此绑定的唯一列是名称。我想做的是将x,y信息加入table_1三次。每个区域唯一的一栏。

  TABLE_1
    NAME    TYPE
    LINE_1  A
    LINE_2  B

  TABLE_2
    NAME    ZONE    X       Y
    LINE_1  ZONE_1  53.36   48.99
    LINE_2  ZONE_1  4.36    81.38
    LINE_1  ZONE_2  41.11   93.85
    LINE_2  ZONE_2  92.86   5.37
    LINE_1  ZONE_3  3.44    87.41
    LINE_2  ZONE_3  24.45   78.60


  TABLE_OUT
    NAME    TYPE    X_ZONE_1    Y_ZONE_1    X_ZONE_2    Y_ZONE_2    X_ZONE_3    Y_ZONE_3
    LINE_1  A       53.36       48.99       41.11       93.85       3.44        87.41
    LINE_2  B       4.36        81.38       92.86       5.37        24.45       78.60

我可以做一个区域确定...

SELECT A.NAME, B.X AS X_ZONE_1, B.Y AS Y_ZONE_2 FROM
TABLE_1 A, TABLE_2 B
WHERE A.NAME = B.NAME 
AND B.ZONE = '1'

2 个答案:

答案 0 :(得分:1)

作为一个新手你应该学习使用SQL。最糟糕的情况是你得到编译错误; - )

通过一些实验,您可能已经找到了这个:

SELECT A.NAME, B1.X AS X_ZONE_1, B1.Y AS Y_ZONE_1 
             , B2.X AS X_ZONE_2, B2.Y AS Y_ZONE_2
             , B3.X AS X_ZONE_3, B3.Y AS Y_ZONE_3 
FROM  TABLE_1 A, TABLE_2 B1
      , TABLE_2 B2
      , TABLE_2 B3 
WHERE A.NAME = B1.NAME 
AND B1.ZONE = '1'
AND A.NAME = B2.NAME 
AND B2.ZONE = '2'
AND A.NAME = B3.NAME 
AND B3.ZONE = '3'

请注意,我们需要这些表和列别名才能使其正常工作。

  

“这主要是有效的,但它返回了太多记录。”

如果没有看到实际的SQL和一些有代表性的输入数据,我无法评论您的实际SQL。但总的来说,太多行是由于:

  • 缺少连接表达式
  • 缺少过滤器表达式

调试查询的方法是逐步构建它们。从TABLE_1开始;确保它返回一行。然后将其加入TABLE_2。仍然得到一排?加入TABLE_3。重复,直到得到你不期望的结果。逐步构建SQL意味着你不太可能引入拼写错误,并且当你这样做时更容易发现它们。

请注意,此处发布的示例没有TABLE_1的过滤器,因此它将为驱动表中的每一行返回至少一行。如果NAMETYPE之间没有一对一的对应关系,那么NAME的每个值都会得到多个结果。

ANSI 92连接语法有助于复杂查询。它将连接和过滤器分开,许多人发现它们更具可读性:

SELECT A.NAME, B1.X AS X_ZONE_1, B1.Y AS Y_ZONE_1 
             , B2.X AS X_ZONE_2, B2.Y AS Y_ZONE_2
             , B3.X AS X_ZONE_3, B3.Y AS Y_ZONE_3 
FROM  TABLE_1 A
          join TABLE_2 B1 on B1.NAME = A.NAME
          join TABLE_2 B2 on B2.NAME = A.NAME
          join TABLE_2 B3 on B2.NAME = A.NAME
WHERE B1.ZONE = '1'
AND B2.ZONE = '2'
AND B3.ZONE = '3'

答案 1 :(得分:0)

仅使用一个联接:

SQL Fiddle

Oracle 11g R2架构设置

private void DrivingHistoryListView_ItemClick(object sender, ItemClickEventArgs e)
{
    var SelectedItem = (sender as ListView).SelectedItem;
    .....
}

查询1

CREATE TABLE TABLE_1 ( NAME, TYPE ) AS
          SELECT 'LINE_1', 'A' FROM DUAL
UNION ALL SELECT 'LINE_2', 'B' FROM DUAL;

CREATE TABLE TABLE_2 (NAME, ZONE, X, Y ) AS
          SELECT 'LINE_1', 'ZONE_1', 53.36, 48.99 FROM DUAL
UNION ALL SELECT 'LINE_2', 'ZONE_1',  4.36, 81.38 FROM DUAL
UNION ALL SELECT 'LINE_1', 'ZONE_2', 41.11, 93.85 FROM DUAL
UNION ALL SELECT 'LINE_2', 'ZONE_2', 92.86,  5.37 FROM DUAL
UNION ALL SELECT 'LINE_1', 'ZONE_3',  3.44, 87.41 FROM DUAL
UNION ALL SELECT 'LINE_2', 'ZONE_3', 24.45, 78.60 FROM DUAL;

<强> Results

SELECT t1.NAME,
       t1.TYPE,
       MIN( CASE ZONE WHEN 'ZONE_1' THEN X END ) AS X_ZONE_1,
       MIN( CASE ZONE WHEN 'ZONE_1' THEN Y END ) AS Y_ZONE_1,
       MIN( CASE ZONE WHEN 'ZONE_2' THEN X END ) AS X_ZONE_2,
       MIN( CASE ZONE WHEN 'ZONE_2' THEN Y END ) AS Y_ZONE_2,
       MIN( CASE ZONE WHEN 'ZONE_3' THEN X END ) AS X_ZONE_3,
       MIN( CASE ZONE WHEN 'ZONE_3' THEN Y END ) AS Y_ZONE_3
FROM   TABLE_1 t1
       INNER JOIN
       TABLE_2 t2
       ON ( t1.NAME = t2.NAME )
GROUP BY
       T1.NAME,
       T1.TYPE