如何从表2中为表1中的每一行获取一行(具有Blob列)?

时间:2019-02-14 07:39:47

标签: sql oracle

我正在使用此查询从t2中获取与t1中的主键匹配的第一行,但是由于找不到正确的查询方式,我没有得到正确的结果

select t1.id as id , t1.title as title ,t2.fId, t2.image as image ,t2.fName as fName ,t2.fType as fType
         from (select data_TBL.ID as id ,data_TBL.INT_STATUS as status,data_TBL.int_TYPE as type , data_TBL.TXT_TITLE as title ,data_TBL.dat_trans_date as cDate from data_TBL  
         ORDER BY  dat_trans_date DESC ) t1,
         (select int_data_id as fId ,int_category as category ,txt_attach_type as fType,txt_filename as fName,blob_file as image from data_attach_tbl
         where  int_category=1 and ROWNUM=1 and data_attach_tbl.int_data_id = id) t2
         where 
          t1.type = 11
         AND t1.status >= 1
         and ROWNUM <=6

查询条件:

  1. 结果应按降序排列
  2. 对于t1中的每一行,从t2中获取一行(data_attach_tbl.int_data_id = id)
  3. 6行是最终结果

样本

t1     id     date            vac_title 
       1      15/10/2018      test 1
       2      20/10/2018      test 2
       3      21/10/2018      test 3
       4      22/10/2018      test 4
       5      23/10/2018      test 5

t2     id      t1_Id        file       category
       1         2          image 1      1
       2         2          image 5      1
       3         4          image 10     1
       4         4          text file    2
       5         4          image 3      1
       6         5          image 2      1

结果应为

      t1_id   date            vac_title     file
       5      23/10/2018      test 5       image 2
       4      22/10/2018      test 4       image 10
       2      20/10/2018      test 2       image 5

按日期对结果进行排序,并从t2中获得匹配的第一行,并且类别= 1

from子句中的第二条select语句找不到与id匹配的相关行

谢谢

2 个答案:

答案 0 :(得分:0)

我认为类似的结果应该会得到:

SELECT t1.id as id , t1.title as title ,t2.fId, t2.image as image ,t2.fName as fName, t2.fType as fType
FROM (SELECT i1.id , i1.title, MIN(i2.Id)  as minID -- t2 primary key field
    FROM data_TBL i1
    INNER JOIN data_attach_tbl i2 on i1.id = i2.int_data_id  -- t1 foreign key field in t2
    WHERE i2.int_category = 1
    GROUP BY i1.id ) t1
INNER JOIN data_attach_tbl t2 on t1.minID = t2.Id -- t2 primary key field
ORDER BY t1.date desc;

我已经忽略了t1的WHERE子句的类型,状态和ROWNUM,因为您没有提到它们,如果需要它们,应该很容易将它们重新添加到内部查询中。

请注意,我尚未在Oracle中进行测试,但这是标准SQL,因此希望可以。

答案 1 :(得分:0)

刚刚找到了解决我问题的方法,希望对某人有用。

SELECT
t1.title AS title,
t2.int_data_id,
t2.blob_file AS image,
t2.txt_attach_name AS fname,
t2.txt_attach_type AS ftype,
t1.cdate,
t1.id AS aid
FROM
    (
        SELECT
            data_tbl.id AS id,
            data_tbl.int_status AS status,
            data_tbl.int_type AS type,
            data_tbl.txt_title AS title,
            data_tbl.dat_trans_date AS cdate
        FROM
            data_tbl
        ORDER BY
            dat_trans_date DESC
    ) t1
    INNER JOIN (
        SELECT
            id,
            int_data_id,
            int_category,
            txt_attach_name,
            blob_file,
            txt_attach_type,
            rownumber
        FROM
            (
                SELECT
                    id,
                    int_data_id,
                    int_category,
                    txt_attach_name,
                    txt_attach_type,
                    blob_file,
                    ROW_NUMBER() OVER(
                        PARTITION BY int_data_id
                        ORDER BY
                            int_data_id DESC
                    ) AS rownumber
                FROM
                    data_attach_tbl
                WHERE
                    int_category = 1
            ) d
        WHERE
            d.rownumber = 1
        ORDER BY
            int_data_id DESC
    ) t2 ON t1.id = t2.int_data_id
            WHERE t1.type = 11
            AND t1.status >= 1;