0

これは一日中私を悩ませています:

1テーブルで2カウントやりたい。1 つ目はショップの総数をカウントし、2 つ目は tsuppliers.lowestprice よりも価格が低いショップの総数をカウントする必要があります。

したがって、基本的には、2 番目のクエリを最初のクエリに結合したいと考えています。

SELECT tshops.shopID, tshops.OfficialName, tsuppliershopinfo.ContactName,   tsuppliershopinfo.ContactMail, Count(distinct tresults.pID) AS AantalVanpID
FROM (tsupplierproducts 
    INNER JOIN tresults ON tsupplierproducts.pID = tresults.pID)     
    INNER JOIN (tsuppliershopinfo INNER JOIN tshops ON tsuppliershopinfo.shopID = tshops.shopID) ON tresults.shopID = tsuppliershopinfo.shopID
WHERE (((tsupplierproducts.supplierID)=2)) AND tresults.starttime BETWEEN DATE_SUB(CURDATE(), INTERVAL 14 DAY) AND DATE_ADD(CURDATE(),INTERVAL 1 DAY)
GROUP BY tshops.shopID, tshops.OfficialName, tsuppliershopinfo.ContactName, tsuppliershopinfo.ContactMail;

2 つ目 (where 句に余分な条件が 1 つだけあることに注意してください):

SELECT tresults.shopID, Count(distinct tresults.pID) AS AantalVanpID
FROM tsupplierproducts INNER JOIN tresults ON tsupplierproducts.pID = tresults.pID
WHERE (((tsupplierproducts.supplierID)=2) AND ((tresults.Price)<tsupplierproducts.LowestPrice)) AND tresults.starttime BETWEEN DATE_SUB(CURDATE(), INTERVAL 14 DAY) AND DATE_ADD(CURDATE(),INTERVAL 1 DAY)
GROUP BY tresults.shopID;

2 番目のクエリを最初のクエリに結合するにはどうすればよいですか?

ありがとう!

4

1 に答える 1

1
SELECT 
    tshops.shopID, 
    tshops.OfficialName, 
    tsuppliershopinfo.ContactName,   
    tsuppliershopinfo.ContactMail, 
    Count(distinct tresults.pID) AS AantalVanpID,
    Count(distinct 
        case when tresults.Price < tsupplierproducts.LowestPrice then tresult.pID end
        ) as AantalVanpID_2
FROM
    tsupplierproducts 
    INNER JOIN tresults ON tsupplierproducts.pID = tresults.pID   
    INNER JOIN tsuppliershopinfo ON tresults.shopID = tsuppliershopinfo.shopID
    INNER JOIN tshops ON tsuppliershopinfo.shopID = tshops.shopID
WHERE 
    tsupplierproducts.supplierID = 2
    AND 
    tresults.starttime BETWEEN 
        DATE_SUB(CURDATE(), INTERVAL 14 DAY) AND DATE_ADD(CURDATE(),INTERVAL 1 DAY)
GROUP BY 
    tshops.shopID, 
    tshops.OfficialName, 
    tsuppliershopinfo.ContactName, 
    tsuppliershopinfo.ContactMail
于 2012-10-14T13:48:20.587 に答える