jag har de här tabellerna: En rad kan inte uppfylla dbo.tblAttributes.AID = 1 och dbo.tblAttributes.AID = 5 sammtidigt.sql-sats problem
tblproducts:
pID
PGID
tblProductsAttributesValues:
PAID
AID
PID
[value]
tblAttributes:
AID
PGID
shown
försöker köra denna frågan:
SELECT DISTINCT dbo.tblProductsAttributesValues.PID FROM dbo.tblProductsAttributesValues INNER JOIN dbo.tblAttributes ON dbo.tblProductsAttributesValues.AID = dbo.tblAttributes.AID INNER JOIN dbo.tblProducts ON dbo.tblProductsAttributesValues.PID = dbo.tblProducts.PID WHERE dbo.tblAttributes.Shown = 1 AND dbo.tblProducts.PGID IN (1,2,3,4,5,7,8,9,22) AND (dbo.tblAttributes.AID = 1 AND dbo.tblProductsAttributesValues.[Value] LIKE '%f%') AND (dbo.tblAttributes.AID = 5 AND CONVERT(INT, dbo.tblProductsAttributesValues.[Value]) = 5)
men får inte ut något resultat...
om jag kör de olika WHERE-villkoren separat så får jag värden:
1.
SELECT DISTINCT dbo.tblProductsAttributesValues.PID FROM dbo.tblProductsAttributesValues INNER JOIN dbo.tblAttributes ON dbo.tblProductsAttributesValues.AID = dbo.tblAttributes.AID INNER JOIN dbo.tblProducts ON dbo.tblProductsAttributesValues.PID = dbo.tblProducts.PID WHERE dbo.tblAttributes.Shown = 1 AND dbo.tblProducts.PGID IN (1,2,3,4,5,7,8,9,22) AND (dbo.tblAttributes.AID = 1 AND dbo.tblProductsAttributesValues.[Value] LIKE '%f%')
returnerar ett antal PID
2.
SELECT DISTINCT dbo.tblProductsAttributesValues.PID FROM dbo.tblProductsAttributesValues INNER JOIN dbo.tblAttributes ON dbo.tblProductsAttributesValues.AID = dbo.tblAttributes.AID INNER JOIN dbo.tblProducts ON dbo.tblProductsAttributesValues.PID = dbo.tblProducts.PID WHERE dbo.tblAttributes.Shown = 1 AND dbo.tblProducts.PGID IN (1,2,3,4,5,7,8,9,22) AND
(dbo.tblAttributes.AID = 5 AND CONVERT(INT, dbo.tblProductsAttributesValues.[Value]) = 5)
returnerar ett antal PID
fast när jag försöker slå ihop dem fungerar det inte...är det nån som ser nåt fel??
/AndreasSv: sql-sats problem
Om du vill begränsa urvalet är du tvungen att använda två joins eller subquerys.
SELECT dbo.tblProducts.PID
FROM dbo.tblProducts
WHERE dbo.tblProducts.PGID IN (1,2,3,4,5,7,8,9,22) AND
dbo.tblProductsAttributesValues.PID IN (SELECT dbo.tblProductsAttributesValues.PID
FROM dbo.tblAttributes LEFT JOIN dbo.tblAttributes ON dbo.tblProductsAttributesValues.AID = dbo.tblAttributes.AID
WHERE dbo.tblAttributes.Shown = 1 AND (dbo.tblAttributes.AID = 1 AND dbo.tblProductsAttributesValues.[Value] LIKE '%f%')) AND
dbo.tblProductsAttributesValues.PID IN (SELECT dbo.tblProductsAttributesValues.PID
FROM dbo.tblAttributes LEFT JOIN dbo.tblAttributes ON dbo.tblProductsAttributesValues.AID = dbo.tblAttributes.AID
WHERE dbo.tblAttributes.Shown = 1 AND (dbo.tblAttributes.AID = 5 AND CONVERT(INT, dbo.tblProductsAttributesValues.[Value]) = 5))
Kan vara snabbare att lägga dem i serie:
SELECT dbo.tblProductsAttributesValues.PID
FROM dbo.tblAttributes LEFT JOIN dbo.tblAttributes ON dbo.tblProductsAttributesValues.AID = dbo.tblAttributes.AID
WHERE dbo.tblAttributes.Shown = 1 AND (dbo.tblAttributes.AID = 1 AND dbo.tblProductsAttributesValues.[Value] LIKE '%f%') AND
dbo.tblProductsAttributesValues.PID IN (SELECT dbo.tblProductsAttributesValues.PID
FROM dbo.tblAttributes LEFT JOIN dbo.tblAttributes ON dbo.tblProductsAttributesValues.AID = dbo.tblAttributes.AID
WHERE dbo.tblAttributes.Shown = 1 AND (dbo.tblAttributes.AID = 5 AND CONVERT(INT, dbo.tblProductsAttributesValues.[Value]) = 5) AND
dbo.tblProductsAttributesValues.PID IN (SELECT dbo.tblProducts.PID
FROM dbo.tblProducts
WHERE dbo.tblProducts.PGID IN (1,2,3,4,5,7,8,9,22))