Jump to content

SQL is rusty


play_

Recommended Posts

Not sure if what I am trying to do is possible. It's gonna be a bit hard to explain but it's actually simple.

 

Ok so first off, here's what  an example paste(it's a pastebin site):

http://tabbit.org/9aEXPo

http://tabbit.org/-pua

 

What I need help with is the 'recent' link, where I would like to display the last 10 or so pastes.

The complication arises with the fact the pastebin has 'tabs'.

 

MySQL tables are set up as such.

 

mysql.jpg

 

When you make a new paste, it's id gets created in the table 'ids' as 'paste_id'.

Each tab in the paste gets inserted into 'pastes' table, and it connects to the 'ids' table via paste_id.

 

So for the 'recent' page, I am trying to gather some of the latest pastes.

 

Right now the recent just prints an array that the query returns. If you go there right now, you'll see

Array
(
    [pastes] => Array
        (
            [0] => Body of tab 1, paste 1
            [1] => Body of tab 2, paste 1
            [2] => Body of tab1, paste 2
            [3] => Body of tab2, paste 2
        )

    [tabs] => Array
        (
            [0] => Tab1
            [1] => Tab2
            [2] => Tab1
            [3] => Tab2
        )

)

 

Ideally, I would want MySQL to return an array(paste_id) that contains 2 inner arrays - one holding the tab names, and the other holding the body of the paste

 

array =>
    [paste_id (1)]
        [tabs]
            [0] tab1
            [1] tab2
        [paste]
            [0] <?php .... ?>
            [1] <? php code or anything ?>

    [paste_id (2)]
        [tabs]
            [0] tab1
            [1] tab2
        [paste]
            [0] import twisted
            [1] from some.module import *

 

is it possible?

Link to comment
Share on other sites

As of right now, there is none.

I have tried all joins that I could. I am not too knowledgeable with complex queries, so i came here.

 

One alternative is to use GROUP_CONCAT...but since it's for a pastebin site, i couldn't really risk using it since the separator could be in someone's paste where pastes.paste_id = $result['ids.paste_id']  <-- not syntax correct, but just to give the idea.

 

 

alternative 2 is to run two queries. one to get the last 10 paste_id from 'ids'., and then for each id, do a select * from

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.