Jump to content

Recommended Posts

What I'm trying to do is group by supplies but return 2 of each and than sort by date

 

 

TABLE NAME : FRUIT

 

supplies | type |  date

 

4 |    apple    |    1357656565

4 |    orange    |    123333321

4 |    pear      |    1212312321

6 |    grape        |  1312312321

6 |    grapefuit    |  13121312321

 

Results in :

 

4 | apple | 1357656565

4 | orange | 123333321

6 |    grape        |  1312312321

6 |    grapefuit    |  13121312321

 

 

For the life of me I can't get my brain to wrap around it.  I'm pretty sure I've done this before and it had to be done by using a subquery.

 

Can anyone help?

Link to comment
https://forums.phpfreaks.com/topic/267673-2-rows-for-each/
Share on other sites

What I would like is it to be sorted by date ASC but get 2 results for each supply.  Here I have a better example that people can understand better:

 

 

Table : Comments

post_idcommentdate

123Hi there1312323443

123How are you131232454

123How are you Jim1312342454

146How are you131232454

146How are you131232454

 

 

Output :

 

post_idcommentdate

123How are you131232454

123How are you Jim1312342454

146How are you131232454

146How are you131232454

 

 

 

So in this example, I would want to take 2 of the latest comments by each post

 

Link to comment
https://forums.phpfreaks.com/topic/267673-2-rows-for-each/#findComment-1373079
Share on other sites

I think I got it working properly using (a modified version of) the query from the following page

 

http://stackoverflow.com/questions/2596489/how-to-select-the-last-two-records-for-each-topic-id-in-mysql

 

Ah...., that's different. You got it.

Now you have a unique id for each record, that's why I said "to redesign a database structure"  :shy:

Link to comment
https://forums.phpfreaks.com/topic/267673-2-rows-for-each/#findComment-1373203
Share on other sites

Yeah I had the idea of doing something similar to this, I just couldn't figure out how to do it properly.

For future reference to anyone viewing this thread, it does work

Yep, this will work with one table and an unique id, but the query is a little complex:

Take a look at example:

+----+---------+-----------------+------------+

| id | post_id | comment        | date      |

+----+---------+-----------------+------------+

|  1 |      1 | Hi there        | 1312323443 |

|  2 |      1 | How are you    | 131232454  |

|  3 |      1 | How are you Jim | 1312342454 |

|  4 |      2 | How are you    | 131232454  |

|  5 |      2 | How are you    | 131232454  |

|  6 |      1 | New comment    | 1234245    |

+----+---------+-----------------+------------+

 

SELECT `c`.`id`, `c`.`comment`, FROM_UNIXTIME(`date`) as `timeDate`
FROM `comments` `c`
WHERE `c`.`id` IN (
    SELECT MAX(`c1`.`id`)
    FROM `comments` `c1`
    WHERE `c`.`post_id` = `c1`.`post_id`
              ) OR`c`.`id` IN (
    SELECT MAX(`c1`.`id`)
    FROM `comments` `c1`
    WHERE `c`.`post_id` = `c1`.`post_id` AND 
    `c1`.`id` NOT IN (
        SELECT MAX(`c2`.`id`)
        FROM `comments` `c2`
        WHERE `c1`.`post_id` = `c2`.`post_id`
                )
              )       
ORDER BY `c`.`post_id`, `c`.`id`

 

RESULT:

 

+----+-----------------+---------------------+

| id | comment        | timeDate            |

+----+-----------------+---------------------+

|  2 | How are you    | 1974-02-27 16:27:34 |

|  3 | How are you Jim | 2011-08-02 23:34:14 |

|  4 | How are you    | 1974-02-27 16:27:34 |

|  5 | How are you    | 1974-02-27 16:27:34 |

+----+-----------------+---------------------+

Link to comment
https://forums.phpfreaks.com/topic/267673-2-rows-for-each/#findComment-1373410
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.