Author Topic: [SOLVED] its new year and I am stuck!!!  (Read 646 times)

0 Members and 1 Guest are viewing this topic.

Offline ToonMarinerTopic starter

  • Fanatic
  • Posts: 3,715
  • Gender: Male
    • View Profile
[SOLVED] its new year and I am stuck!!!
« on: December 31, 2007, 10:16:43 PM »
OK I have no life until this gets done so pretty please help me out!!!!

I have a table or items and I need to select all fields from the table but distinct values from just one field

eg.

many items and numerous occuencies of a category - I want the last entry from each category.

Any help much appreciated.... (I know I have done this before but just can't get it...)
follow me on twitter @PHPsycho

Offline drranch

  • Enthusiast
  • Posts: 78
    • View Profile
Re: its new year and I am stuck!!!
« Reply #1 on: December 31, 2007, 10:23:10 PM »
Try this out...

Code: [Select]
SELECT * FROM tablename ORDER BY id DESC LIMIT 1
HA your not the only one with out a life.  ::)

Offline ToonMarinerTopic starter

  • Fanatic
  • Posts: 3,715
  • Gender: Male
    • View Profile
Re: its new year and I am stuck!!!
« Reply #2 on: December 31, 2007, 10:30:24 PM »
sorry didn't make it clear...

There are many categories 1,2,3,4,5,6,7,8,9 etc etc and multiple entires in each category...

so there could be 20 in cat 3 and 17 in cat 8 what I need is the last entry from each category.

PS I also need to select data from a couple of other tables in the query too but when ever I use more than one field in the select part for the query and have a DISTINCT on one of them the query fails....

Sorry for not being clear.
follow me on twitter @PHPsycho

Offline drranch

  • Enthusiast
  • Posts: 78
    • View Profile
Re: its new year and I am stuck!!!
« Reply #3 on: December 31, 2007, 10:35:09 PM »
What is your current syntax.  Reading syntax helps me to better understand what your looking for.

Offline ToonMarinerTopic starter

  • Fanatic
  • Posts: 3,715
  • Gender: Male
    • View Profile
Re: its new year and I am stuck!!!
« Reply #4 on: December 31, 2007, 11:32:39 PM »
Code: [Select]
SELECT
`media`.`media_id` ,
`media`.`user_id` ,
`media`.`media_key` ,
`media`.`type` ,
`media`.`title` ,
DISTINCT(`media`.`categories`) ,
`user`.`username` ,
`sections`.`url` ,
DATEDIFF(NOW(), `media`.`added`) AS `period`
FROM
`media` ,
`user` ,
`sections`
WHERE
`media`.`flag` != 'm'
AND
`user`.`user_id` = `media`.`user_id`
AND
`user`.`active` = 'y'
ORDER BY
`media`.`added` DESC
follow me on twitter @PHPsycho

Offline Barand

  • Sen . (ile || sei)
  • Staff Alumni
  • 'Mind Boggling!'
  • *
  • Posts: 15,132
  • Gender: Male
  • php 4.3/5.1 MySql 5.0.1
    • View Profile
Re: its new year and I am stuck!!!
« Reply #5 on: January 01, 2008, 05:56:33 AM »
try
Code: [Select]
SELECT
`media`.`media_id` ,
`media`.`user_id` ,
`media`.`media_key` ,
`media`.`type` ,
`media`.`title` ,
`media`.`categories` ,
`user`.`username` ,
`sections`.`url` ,
DATEDIFF(NOW(), `media`.`added`) AS `period`
FROM
`media` ,
`user` ,
`sections`,
                                                            (SELECT categories, MAX(added) as latest
                                                                FROM media GROUP BY categories) as x
WHERE
`media`.`flag` != 'm'
AND
`user`.`user_id` = `media`.`user_id`
AND
`user`.`active` = 'y'
                                                  AND       
                                                             media.categories       =           x.categories
                                                  AND        media.added            =           x.latest
ORDER BY
`media`.`added` DESC
« Last Edit: January 01, 2008, 05:58:17 AM by Barand »
|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

Offline ToonMarinerTopic starter

  • Fanatic
  • Posts: 3,715
  • Gender: Male
    • View Profile
Re: its new year and I am stuck!!!
« Reply #6 on: January 01, 2008, 06:31:04 AM »
Barand...

Thanks for the reply...
That query (I think) does the business but returns multiple duplicate entries.  I'd love a little precis on what the query is doing as I 'kind of' get what its doing but not totally...

ANyway and update...

I tried this...

Code: [Select]
SELECT
DISTINCT(`media`.`categories`) ,
`media`.* ,
`user`.`username` ,
`sections`.`url` ,
DATEDIFF(NOW(), `media`.`added`) AS `period`
FROM
`media` ,
`user` ,
`sections`
WHERE
`media`.`flag` != 'm'
AND
`user`.`user_id` = `media`.`user_id`
AND
`user`.`active` = 'y'
GROUP BY
`media`.`categories`
ORDER BY
`media`.`added` DESC


with some success (it appears the distinct coming first in the select clause is important).

How ever it selects the earliest records in the dataset rather than the latest.  Would anyone be so kind as to suggest why?
follow me on twitter @PHPsycho

Offline Barand

  • Sen . (ile || sei)
  • Staff Alumni
  • 'Mind Boggling!'
  • *
  • Posts: 15,132
  • Gender: Male
  • php 4.3/5.1 MySql 5.0.1
    • View Profile
Re: its new year and I am stuck!!!
« Reply #7 on: January 01, 2008, 06:51:24 AM »
SELECT a, b, c, SUM(d) FROM table GROUP BY a

When using GROUP BY you should group by all selected columns that are not aggregated (ie not SUM(), COUNT() etc) and SQL normally enforces this. MySQL is lenient and lets you group by a single col as in the example above.

In this case the manual states that the values of b and c will be unpredictable but they usually are taken from the first record in each group.

+ + +

You can think of the subquery as a temporary table called x which contains the latest date for each category from the media table


             media                        subquery
             id    cat   date             cat     maxdate
             ----+-----+-------------     -----+-------------
              1     1    2007-01-01         
              2     1    2007-01-02         
              3     1    2007-01-03  <--->  1     2007-01-03
              4     2    2007-01-01
              5     2    2007-01-02         
              6     2    2007-01-04  <--->  2     2007-01-04


Joining to this should identify the latest record for each category

A thought just occured - if you have several recs on that last date it pulls them all. May have to use MAX(id) in the subquery and match on that instead of date.
|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

Offline ToonMarinerTopic starter

  • Fanatic
  • Posts: 3,715
  • Gender: Male
    • View Profile
Re: its new year and I am stuck!!!
« Reply #8 on: January 01, 2008, 08:09:29 AM »
This is the query I ended up with (just a couple of minor alterations in field names etc) and it works like a charm.

Code: [Select]
SELECT
`media`.* ,
`user`.`username` ,
`sections`.`url` ,
`sections`.`string_key` ,
DATEDIFF(NOW(), `media`.`added`) AS `period`
FROM
`media` ,
`user` ,
`sections`,
                        (
SELECT
DISTINCT(`categories`) ,
MAX(`media_id`) as `latest`
FROM
`media`
GROUP BY
`categories`
) AS `x`
WHERE
`media`.`flag` != 'm'
AND
`user`.`user_id` = `media`.`user_id`
AND
`user`.`active` = 'y'
AND       
`media`.`categories`       =           `x`.`categories`
AND
`media`.`media_id`            =           `x`.`latest`
AND
`sections`.`section_id` = `media`.`categories`
ORDER BY
`media`.`added` DESC



MANY THANKS!!!!
follow me on twitter @PHPsycho