Jump to content

Intercalating results


dhulke

Recommended Posts

Hi!

So I have a pretty huge database of clients and each client has a number of objects. When people want to search for these objects on my website, I'd like to show the results intercalated by client. As of now, I'm getting like 5 rows of client A and then another 3 of client B and so forth. I'd like to get something like: First object of client A, First object of client B, First object of client C, Second object of client A, Second object of client B... and so on.

 

Since my results are paginated and there are a freking whole lot of them, I can't do it programmatically at runtime, not only for speed's sake, but because I could easily run out of memory. The way I've been doing it is to create a field "order" on my object table and every night I run a script that orders the objects the way I want. Is there a better way of doing this?

 

Thanks a lot.

Link to comment
Share on other sites

You didn't state whether all clients have at least one object and, if they don't, whether you want clients without any objects to be displayed. If all clients have objects OR if you don't want to show ones that don't ahve any objects, remove the "LEFT" part of the JOIN. This is just an example since I don't know your table structure. It will get one object for each client based on the object with the lowest ID.

 

This would select only the first object

 

 

SELECT *
FROM clients
LEFT JOIN objects
    ON clients.client_id = objects.client_id
    AND objects.object_id IN
    (
        SELECT MIN(object_id)
        FROM objects
        GROUP BY client_id
    )
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.