All pastes #523930 Raw Edit

mrgenixus

public sql v1 · immutable
#523930 ·published 2007-05-30 23:02 UTC
rendered paste body
SELECT (@pos:=@pos+1) AS ord,                  coupon.id AS CID,                  coupon.name AS name,                  UNIX_TIMESTAMP(coupon.effective) AS effective,                  UNIX_TIMESTAMP(coupon.expiration) AS expiration,                  coupon.id AS x,                  ( SELECT ( ( SELECT count( * )  FROM `vote_coupon` WHERE vote_coupon.coupon_id = x ) * .75 ) + ( ( SELECT ( 30 - ( to_days( expiration ) - to_days( now() ) ) )                    FROM `coupon`                    WHERE ( expiration - now() ) <= 30                      AND coupon.id = x                    GROUP BY coupon.id )  * .15 ) + ( ( SELECT count( * ) FROM `access` WHERE access.coupon_id = x ) * .10 ) ) AS score                FROM `coupon` , `subcategory` , `category` , `business`                WHERE coupon.effective <= now()                        AND coupon.expiration >= now()                        AND (SELECT (coupon.flags ^ 1)) #ACTIVE                GROUP BY coupon.id                HAVING score > 4.20                LIMIT 0,10with the 'HAVING' statement+------+-----+-----------------------+------------+------------+---+--------+| ord  | CID | name                  | effective  | expiration | x | score  |+------+-----+-----------------------+------------+------------+---+--------+| NULL |   1 | Buy One Get One Free! | 1179986400 | 1180591200 | 1 | 113.65 | | NULL |   5 | Hail To the Cheif     | 1180418400 | 1180591200 | 5 |   4.35 | +------+-----+-----------------------+------------+------------+---+--------+without the 'HAVING' statement this returns..and Returns: +------+-----+----------------------------+------------+------------+---+--------+| ord  | CID | name                       | effective  | expiration | x | score  |+------+-----+----------------------------+------------+------------+---+--------+| NULL |   1 | Buy One Get One Free!      | 1179986400 | 1180591200 | 1 | 113.65 | | NULL |   3 | Free Medium Drink w/Entree | 1180159200 | 1180677600 | 3 |   4.20 | | NULL |   5 | Hail To the Cheif          | 1180418400 | 1180591200 | 5 |   4.35 | +------+-----+----------------------------+------------+------------+---+--------+so instead of just listing these, i'd like to count them but the statement below doesn't work:SELECT coupon.id AS x,                count (*)                FROM `coupon` , `subcategory` , `category` , `business`                WHERE coupon.effective <= now()                        AND coupon.expiration >= now()                        AND (SELECT (coupon.flags ^ 1)) #ACTIVE                GROUP BY coupon.id                HAVING (                	SELECT                 	(                 		(                 			SELECT count( * )                  			FROM `vote_coupon`                 			WHERE vote_coupon.coupon_id = x                 		)                 		*                 		.75                	)                	+                	(                 		(                			SELECT                 			(                 				30                 				-                 				(                 					to_days( coupon.expiration )                 					-                 					to_days( now() )                 				)                			)                    		FROM `coupon`                    		WHERE ( coupon.expiration - now() ) <= 30                      		AND coupon.id = x                    		GROUP BY coupon.id                     	)                      	*                    	.15                    )                    +                    (                    	(                    		SELECT count( * )                     		FROM `access`                     		WHERE access.coupon_id = x                    	)                     	*                     	.10                     ) 				) > 4.20                LIMIT 0,10those of you with more experience will probably tell me why mysql outputs:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*)                FROM `coupon` , `subcategory` , `category` , `business`     ' at line 2