0

你们能帮我用子查询重写它吗

SELECT 
    S.DENUMIRE, M.DENUMIRE, AN
FROM 
    SPECIALIZARE S
JOIN 
    MATERII M ON S.CODSPEC = M.CODSPEC
ORDER BY 
    S.DENUMIRE
4

4 回答 4

1
SELECT 
    S.DENUMIRE,
    M.DENUMIRE,
    AN
FROM SPECIALIZARE S
JOIN (
    SELECT
        DENUMIRE,
        CODSPEC
    FROM MATERII
) AS M
    ON S.CODSPEC = M.CODSPEC
ORDER BY S.DENUMIRE
于 2012-12-13T08:48:57.780 回答
0

假设DENUMIRE是一个整数:

SELECT S.DENUMIRE, 1 DENUMIRE, AN
  FROM SPECIALIZARE S
 WHERE EXISTS (
               SELECT * 
                 FROM MATERII M 
                WHERE S.CODSPEC = M.CODSPEC
                      AND M.DENUMIRE = 1
              )
UNION
SELECT S.DENUMIRE, 2 DENUMIRE, AN
  FROM SPECIALIZARE S
 WHERE EXISTS (
               SELECT * 
                 FROM MATERII M 
                WHERE S.CODSPEC = M.CODSPEC
                      AND M.DENUMIRE = 2
              )
UNION
SELECT S.DENUMIRE, 3 DENUMIRE, AN
  FROM SPECIALIZARE S
 WHERE EXISTS (
               SELECT * 
                 FROM MATERII M 
                WHERE S.CODSPEC = M.CODSPEC
                      AND M.DENUMIRE = 3
              )

UNION
...

对于每个整数值,依此类推……这当然是不可行的。

你需要一个加盟!如果您只是想避免JOIN出于教学原因避免使用该关键字:

SELECT 
    S.DENUMIRE, M.DENUMIRE, AN
FROM 
    SPECIALIZARE S, MATERII M 
WHERE
    S.CODSPEC = M.CODSPEC
ORDER BY 
    S.DENUMIRE

ps 虽然 SQL 的“兼容性桎梏”(1980 年代的供应商支持这一点并且它成为标准等)允许同一个表中的两个列具有相同的名称,但这在实践中可能不是一个好主意。另外,我假设AN来自表格SPECIALIZARE,但可能是错字。

于 2012-12-13T09:05:07.327 回答
0

你也许可以用一个 union 和一个 group by 做一些愚蠢的事情。例如:

select max(s_denumire) as s_denumire,
       max(m_denumire) as m_denumire,
       codspec
    from (
        select denumire as s_denumire, null as m_denumire, codspec 
            from specializare
        union all
        select null as s_denumire, denumire as m_denumire, codspec 
            from materii
    )
    group by codspec

这会产生一行 per codspec,所以它不完全像一个连接。但是你可能可以用行号做一些事情来解决这个问题。我会把它作为练习留给你,因为这显然是家庭作业。

于 2012-12-13T13:12:48.460 回答
0

您只想要两个表中的不同行吗?

SELECT
    SQ.DENUMIRE, SQ.AN
FROM (
    SELECT
        DENUMIRE, AN
    FROM 
        SPECIALIZARE

    UNION

    SELECT
        DENUMIRE, AN
    FROM 
        MATERII
) SQ
ORDER BY
    SQ.DENUMIRE
于 2012-12-13T17:28:43.817 回答