0

我很难从子查询中提取值。

假设我有以下表格:

D(fid, pid, r),

F(fid,t,j)

在第一次选择时,我需要来自下面子查询的 fj。

SELECT pid -- Here I need f.j to show up
FROM D
WHERE r='something' AND fid IN
            (
            SELECT f2.fid
            FROM F f2, 
                (
                SELECT f.j, COUNT(*) -- I need f.j above
                FROM F f
                GROUP BY f.j
                HAVING COUNT(*) >=2
                ) f
            WHERE f.j = f2.j
            )
GROUP BY pid
HAVING COUNT(*) >= 2

谢谢。

4

4 回答 4

0

我会以这种形式创建一个临时表T( pid , j )。然后你可以分别插入所有的pid和fj数据。

于 2013-08-14T12:11:15.883 回答
0
SELECT d.pid, f.j, count(f.*) as count
FROM d inner join f
on d.fid = p.fid
WHERE d.r = "something"
GROUP by d.pid, f.j
HAVING count(f.*) >=- 2

请注意,我还没有尝试过这个查询。但是,我想这应该是如何看待的。

于 2013-08-14T12:11:52.553 回答
0

由于没有样本数据,不清楚应该输出什么,您仍然可以尝试以下方法,但这并未优化。

SELECT  pid ,
        t.j -- Here I need f.j to show up
FROM    D
        INNER JOIN ( SELECT f2.fid ,
                            f2.j
                     FROM   F f2 ,
                            ( SELECT    f.j 
                              FROM      F f
                              GROUP BY  f.j
                              HAVING    COUNT(*) >= 2
                            ) f
                     WHERE  f.j = f2.j
                   ) t ON D.fid = t.fid
WHERE   r = 'something'
GROUP BY pid, t.j
HAVING  COUNT(*) >= 2
于 2013-08-14T12:14:00.093 回答
0

大概是这样的:

SELECT D.pid, E.j
FROM D
INNER JOIN (SELECT f1.j
        FROM F f2
        INNER JOIN 
            (
            SELECT f.j, COUNT(*)
            FROM F f
            GROUP BY f.j
            HAVING COUNT(*) >=2
            ) f1
        ON f1.j = f2.j
       ) E
 ON D.fid = E.fid
 WHERE r='something' AND fid IN
        (
        SELECT f2.fid
        FROM F f2, 
            (
            SELECT f.j, COUNT(*) -- I need f.j above
            FROM F f
            GROUP BY f.j
            HAVING COUNT(*) >=2
            ) f
        WHERE f.j = f2.j
        )
 GROUP BY D.pid
 HAVING COUNT(*) >= 2
于 2013-08-14T12:16:49.893 回答