Monday, May 21, 2012

MYSQL WHERE condition depends upon SELECT column

Is it possible to reference back to the column you're selecting in the WHERE condition?



I'm not even sure how to ask this question without excessive verbage, so I'll just try to point you to the "rated_user = @2User" part and note that @2User is what I'm selecting.



SELECT id, user, minimum 
INTO @1ID, @1User, @minimum
FROM table1
WHERE a_or_b = 'a' AND item = itemName
ORDER BY number DESC LIMIT 1;

SELECT id, user
INTO @2ID, @2User
FROM table1
WHERE a_or_b = 'b' AND item = itemName
AND (SELECT IFNULL(AVG(rating),0) AS Rating
FROM table2
WHERE rated_user = @2User AND completed = 'y'
) >= @minimum_seller_rating
ORDER BY number ASC LIMIT 1;




No comments:

Post a Comment