如何转换sql“with”,以便它不使用“with”

时间:2015-12-10 00:27:57

标签: sql where common-table-expression

我需要重写以下使用“with”的查询,以便它不再使用“with”,但我不确定如何完全执行。有人告诉我使用子查询,但我的尝试没有用。

以下是我需要转换的内容:

with
best (cid, title, year, lowest) as (
    select distinct M.cid, O.title, O.year, min(price)
        from yrb_member M, yrb_purchase P, yrb_offer O
        where M.club = O.club and
              M.cid = P.cid and
              P.title = O.title and P.year = O.year
        group by M.cid, O.title, O.year
)
select C.name, P.title, P.year, qnty, price, lowest
from yrb_customer C, yrb_purchase P, Best B, yrb_offer O
where P.cid = B.cid and P.title = B.title and
      P.year = B.year and P.title = O.title and
      P.year = O.year and P.club = O.club and
      C.cid = P.cid and
      O.price > B.lowest
order by C.name, P.title, P.year;

我尝试将with语句的一部分复制到where语句中,但是我收到一条错误,说“标量全选,SELECT INTO语句或VALUES INTO语句的结果不止一行。”您可以在下面看到我的尝试(它仍然使用“with”,但我试图替换一小部分,看看我是否可以在查询的其余部分使用相同的技术,最终完全删除“with”语句):< / p>

with
best (cid, title, year, lowest) as (
    select distinct M.cid, O.title, O.year, min(price)
        from yrb_member M, yrb_purchase P, yrb_offer O
        where M.club = O.club and
              M.cid = P.cid and
              P.title = O.title and P.year = O.year
        group by M.cid, O.title, O.year
)
select distinct C.name, P.title, P.year, qnty, price, lowest
from yrb_customer C, yrb_purchase P, Best B, yrb_offer O
where P.cid =

(select distinct M.cid
        from yrb_member M, yrb_purchase P, yrb_offer O
        where M.club = O.club and
              M.cid = P.cid and
              P.title = O.title and P.year = O.year
        )

and P.title = B.title and
      P.year = B.year and P.title = O.title and
      P.year = O.year and P.club = O.club and
      C.cid = P.cid and
      O.price > B.lowest
order by C.name, P.title, P.year;

1 个答案:

答案 0 :(得分:0)

不确定您使用的是哪个数据库,但通常只需将查询从 function change() { men = document.getElementById("men").value; women = document.getElementById("women").value; document.getElementById('mod').style.width = men + "px"; document.getElementById('mod').style.height = women + "px"; } 直接移到查询的with子句中即可。像这样:

from