Using MySQL variables to limit a select query

I discovered today when writing a stored procedure for a web reporting application that MySQL doesn’t support using variables for the LIMIT clause in a SELECT statement. I didn’t really want to write a dynamic query, one workaround that I found while Googling looked promising:

SET SQL_SELECT_LIMIT = RowLimit (can be a variable)
SELECT ….
SET SQL_SELECT_LIMIT = default

However it is applied at a global level and I needed different limits within a sequence of four queries formed into a union to get the top N results for a series of categories. Because I was giving the user the limited choice of retrieving the top 10, 20, 50 or 100 items I ending up using a CASE statement with each WHEN clause containing a duplicate of the entire query. Certainly not a pretty solution but works and is reasonably maintainable by leaving the LIMIT clause on a seperate line so any changes can be cut & pased down the procedure.

6 Responses to “Using MySQL variables to limit a select query”

  1. SETH says:

    Pillspot.org. Canadian Health&Care.No prescription online pharmacy.Special Internet Prices.PillSpot.org. Herbal-supplements@buy.online” rel=”nofollow”>.…

    Categories: Mens Health.Eye Care.Weight Loss.Antidepressants.Antidiabetic.Anti-allergic/Asthma.Stop SmokingMental HealthSkin Care.Pain Relief.Womens Health.Stomach.Anxiety/Sleep Aid.Antiviral.Antibiotics.Blood Pressure/Heart.Vitamins/Herbal Supple…

  2. FREDDIE says:


    PillSpot.org. Canadian Health&Care.No prescription online pharmacy.Special Internet Prices.Best quality drugs. Online Pharmacy. Buy pills online

    Buy:Actos.Accutane.Mega Hoodia.Zovirax.Lumigan.Human Growth Hormone.Synthroid.Nexium.Retin-A.Valtrex.Petcam (Metacam) Oral Suspension.Zyban.100% Pure Okinawan Coral Calcium.Prednisolone.Prevacid.Arimidex….

Leave a Reply