SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    May 2006
    Location
    Ljubljana
    Posts
    241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    calculating difference with mysql

    Hello,

    I want to run a query to fetch 10 rows from database with the 'category_id' parameter set, and fall back if there are not 10 rows from the mentioned category to fetch the missing rows from the same table without category_id.

    How can you calculate a difference with php?

    Right now I call this query:

    Code:
    (SELECT asin FROM products $where ORDER BY $order_by LIMIT 10)
    	    UNION
    	    (SELECT asin FROM products WHERE site_id = $site_id ORDER BY $order_by LIMIT 10)
    And then use splice_array in php to get the first 10.

    Which would be more elegant way to do this with mysql?

    Thanks for help
    Get RankTrackr 2.0: Rank Tracker | PopStrap

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,412
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by l2u View Post
    Which would be more elegant way to do this with mysql?
    There's nothing wrong with this way, IMO
    I don't know if it would be possible to have MySQL establish how many rows are extracted from the category, and how many rows must be added from the entire table, but even if it is, I'm sure it would be much more complicated than coding that piece of logic in PHP (like you said, all you need there is array_splice).


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •