在子查询中获取Id

我一直在APEX的拍卖网站上工作。对于拍卖网站,我需要做一个概述,说明哪些产品是由某人购买的。肯定它的只是我忘了,但我怎样才能获得结果在以下情况:在子查询中获取Id

3表

Table User 

USER_ID

EMAIL

Table Product

PRODUCT_ID

USER_ID

Table Bid

BID_ID

PRODUCT_ID

USER_ID

CREDITS

Ofcourse有更多的列,但我想,让他们出来,使explenation稍微容易。

最少报名结果

显示产品从产品表。但只有当前用户的出价最高(Credits)。

到目前为止

我一直在想/想在这一个了很多,但在子查询结束不断收到死亡。 (:APP_USER)

SELECT * 

FROM "PRODUCT"

WHERE "PRODUCT_ID" IN (SELECT "PRODUCT_ID" FROM "BID"

WHERE "USER_ID" =

(SELECT "USER_ID" FROM "USER"

WHERE lower("EMAIL") = lower(:APP_USER)))

and "PRODUCT_ID" = (SELECT "PRODUCT_ID" FROM "BID"

WHERE "CREDITS" =

(SELECT MAX("CREDITS") FROM "BID"

WHERE "PRODUCT_ID" = ?

*的Apex从当前用户返回的emailadress

真正的问题在“和”为用户过滤工作正常启动。我不知道如何在子查询中获取比较(当前)的PRODUCT_ID。我很确定,如果我在子查询的末尾得到这个值,它就可以工作。

使用Oracle数据库。

任何帮助表示赞赏:)

回答:

尝试用这个..

SELECT * 

FROM PRODUCT p

WHERE p.PRODUCT_ID IN (SELECT PRODUCT_ID FROM BID"

WHERE "USER_ID" =

(SELECT "USER_ID" FROM "USER"

WHERE lower("EMAIL") = lower(:APP_USER)))

and "PRODUCT_ID" = (SELECT "PRODUCT_ID" FROM "BID" b

WHERE "CREDITS" =

(SELECT MAX("CREDITS") FROM "BID"

WHERE b.PRODUCT_ID" = p.product_id));

and "PRODUCT_ID" in (SELECT "PRODUCT_ID" FROM "BID" b 

WHERE "CREDITS" =

(SELECT MAX("CREDITS") FROM "BID"

WHERE b.PRODUCT_ID" = p.product_id))

回答:

像这样的事情?

,你可能有问题,如果对投标的学分不是每个产品唯一的(但应该是在你的拍卖业务规则反正)

SELECT 

p.*

FROM

Bid b

JOIN

-- get the max bid per product

(SELECT

PRODUCT_ID, MAX(CREDITS) AS MAX_C

FROM

Bid

GROUP BY

PRODUCT_ID) m ON b.PRODUCT_ID = m.PRODUCT_ID AND b.CREDITS = m.CREDITS

JOIN USER u ON b.USER_ID = u.USER_ID

JOIN Product p ON b.PRODUCT_ID = p.PRODUCT_ID

WHERE

lower(u.EMAIL) = lower(:APP_USER)));

注意。

编辑

从查询长度的角度来看,以下为确保您只检查该用户实际已经过投标的产品胜,而不是其上方检查可能会更好所有产品。

SELECT 

p.*

FROM

Bid b

JOIN

-- get the max bid per product that the user bid on

(SELECT

PRODUCT_ID, MAX(CREDITS) AS MAX_C

FROM

Bid b2

JOIN (

-- get only the products that the user has bid on

SELECT

DISTINCT b1.PRODUCT_ID

FROM

Bid b1

JOIN User u1 ON b1.USER_ID = u1.USER_ID

WHERE

lower(u1.EMAIL) = lower(:APP_USER))) dp

ON b2.PRODUCT_ID = dp.PRODUCT_ID

GROUP BY

PRODUCT_ID) m ON b.PRODUCT_ID = m.PRODUCT_ID AND b.CREDITS = m.CREDITS

JOIN USER u ON b.USER_ID = u.USER_ID

JOIN Product p ON b.PRODUCT_ID = p.PRODUCT_ID

WHERE

lower(u.EMAIL) = lower(:APP_USER)));

以上是 在子查询中获取Id 的全部内容, 来源链接: utcz.com/qa/266885.html

回到顶部