我一直在Apex的一个拍卖网站工作。对于拍卖网站,我需要做一个概述,显示哪些产品已被某人购买。很确定这只是我忘记的事情,但在以下情况下,我如何才能得到结果:
3张桌子
Table User
USER_ID
EMAIL
Table Product
PRODUCT_ID
USER_ID
Table Bid
BID_ID
PRODUCT_ID
USER_ID
CREDITS
当然还有更多的专栏,但我认为不写专栏会使解释变得更容易一些。
需要的结果
显示产品表中的产品。但前提是当前用户出价最高(积分)。
到目前为止
我一直在考虑/尝试这个问题,但在子查询结束时总是被杀死。
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返回当前用户的电子邮件地址(:app_user)
真正的问题从“和”开始,因为用户过滤工作正常。我不知道如何在子查询中获取比较的(当前)产品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))