如何在mysql语句中从子查询中获取多列

9avjhtql  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(275)

事实上,我找到了许多解决我的问题的方法,但我不知道如何应用到我的问题!
我的问题是我有以下sql语句:

SELECT
                                `tblacc`.`AccID` AS xAccID,
                                `tblacc`.`AccName` AS xAccName,
                                `tblprod`.`ProductID` AS xProdID,
                                `tblprod`.`ProductName` AS xProdName,
                                (
                                SELECT
                                    tblproductsprices.PriceID
                                FROM
                                    tblproductsprices
                                WHERE
                                    tblproductsprices.PriceProductID = `tblprod`.`ProductID` AND `tblproductsprices`.`PriceResoID` = `tblacc`.`AccID`
                            ) AS xPriceID,
                            (
                            SELECT
                                tblproductsprices.PriceValue
                            FROM
                                tblproductsprices
                            WHERE
                                tblproductsprices.PriceProductID = `tblprod`.`ProductID` AND `tblproductsprices`.`PriceResoID` = `tblacc`.`AccID`
                            ) AS xPriceValue,
                            (
                            SELECT
                                tblproductsprices.PricePallet
                            FROM
                                tblproductsprices
                            WHERE
                                tblproductsprices.PriceProductID = `tblprod`.`ProductID` AND tblproductsprices.PriceResoID = `tblacc`.`AccID`
                            ) AS xPalletPrice
                            FROM
                                tblaccounts tblacc,
                                tblproducts tblprod
                            WHERE
                                tblacc.`AccCat` = 'agent'
                            ORDER BY
                                tblacc.AccName,
                                tblprod.ProductName

我需要的是将以下部件替换为一个部件:

(
                                SELECT
                                    tblproductsprices.PriceID
                                FROM
                                    tblproductsprices
                                WHERE
                                    tblproductsprices.PriceProductID = `tblprod`.`ProductID` AND `tblproductsprices`.`PriceResoID` = `tblacc`.`AccID`
                            ) AS xPriceID,
                            (
                            SELECT
                                tblproductsprices.PriceValue
                            FROM
                                tblproductsprices
                            WHERE
                                tblproductsprices.PriceProductID = `tblprod`.`ProductID` AND `tblproductsprices`.`PriceResoID` = `tblacc`.`AccID`
                            ) AS xPriceValue,
                            (
                            SELECT
                                tblproductsprices.PricePallet
                            FROM
                                tblproductsprices
                            WHERE
                                tblproductsprices.PriceProductID = `tblprod`.`ProductID` AND tblproductsprices.PriceResoID = `tblacc`.`AccID`
                            ) AS xPalletPrice

以相同的条件获取子查询中的所有列?

sd2nnvve

sd2nnvve1#

我不知道你的数据模式。您是否尝试使用表tblproductsprices创建交叉连接(第一个)和左连接?

SELECT
    `tblacc`.`AccID` AS xAccID,
    `tblacc`.`AccName` AS xAccName,
    `tblprod`.`ProductID` AS xProdID,
    `tblprod`.`ProductName` AS xProdName,
    tblproductsprices.PriceID AS xPriceID,
    tblproductsprices.PriceValue AS xPriceValue,
    tblproductsprices.PricePallet AS xPalletPrice

    FROM tblaccounts tblacc

    CROSS JOIN tblproducts tblprod

    LEFT JOIN tblproductsprices ON tblproductsprices.PriceProductID = `tblprod`.`ProductID` AND `tblproductsprices`.`PriceResoID` = `tblacc`.`AccID`

    WHERE tblacc.`AccCat` = 'agent'

    ORDER BY tblacc.AccName, tblprod.ProductName;
7cwmlq89

7cwmlq892#

hi join可用于子查询中使用的表示例查询可通过以下方式-select tblacc . AccID 作为夏奇德, tblacc . AccName 作为你的名字, tblprod . ProductID 作为xprodid, tblprod . ProductName 作为xprodname,tblprodprices.priceid作为xpriceid,tblprodprices.pricevalue作为xpricevalue,tblprodprices.pricepallet作为xpalletprice from tblaccounts tblacc,tblproducts tblprod,tblproductsprices tblprodprices where tblacc。 AccCat ='代理'和tblprodprices.priceproductid= tblprod . ProductID 和tblprodprices.priceresoid= tblacc . AccID 按tblacc.accname、tblprod.productname订购

相关问题