1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Complex SQL query

Discussion in 'Web Design & Development' started by Big-K, Feb 1, 2007.

Thread Status:
Not open for further replies.
Advertisement
  1. Big-K

    Big-K Thread Starter

    Joined:
    Nov 22, 2003
    Messages:
    6,052
    In my current project, i'm using SELECT DISTINCT in a mysql query. What i'm trying to do is get a listing of the different entries that have been used in a field, in descending order, the first entry shown being the entry that was last used. Unfortunately, mysql_query only seems to take the OLDEST instance of that entry, which is the opposite of what I want. Here's the query I have at the current time.

    $get_img = mysql_query("SELECT DISTINCT album FROM img_manager WHERE album != 'other' AND album != 'fractal' order by id desc");

    basically, instead of the query using the oldest known instance of that 'album', i want it to use the newest one for the ordering. Any ideas?
     
  2. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    Hi Big-K,

    There's an error in your SQL statement : id after the ORDER BY is incorrect as it is not part of the selection.

    The correct syntax is :

    SELECT DISTINCT(album) FROM img_manager WHERE album != 'other' AND album != 'fractal' order by 1 desc

    1 will point to the first and only item of your selection which is DISTINCT(album)
     
  3. Big-K

    Big-K Thread Starter

    Joined:
    Nov 22, 2003
    Messages:
    6,052
    The way I was using functions just fine, it still orders them by their id, if I take out that part it goes a different order, and if i do it your way it goes alphabetically(completely not what I'm looking for).
     
  4. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    Without knowing the structure of your table, it's a bit tricky to help you :

    - how do you define an entry is the last used in your table ? By an auto-increment field, by a time field ?
    - duplicate id fields are they allowed ?
    - for each id field, are there many album fields eventually with duplicates ?
     
  5. Big-K

    Big-K Thread Starter

    Joined:
    Nov 22, 2003
    Messages:
    6,052
    its basically a table containing information about images. the fields are as such:

    id
    filename
    album
    datestamp
    some other fields that don't matter

    the id's are in order down the table, with some missing where i've deleted entries. Filenames are completely independent. Album is what sorts them among each other, some are in album A, some in album B, etc. datestamp simply tells the date that the file was upload in a 20070203 format. If i could, datestamp would be the second best field to use for figuring which entry to use in distinction, since it tells how late it was uploaded. Id would be best because they go in numerical order down the line, so time wouldn't be a factor, it is auto-incremented id'ing, and none of them are identical, period. In this whole table, there is not one id that repeats itself, while albums are repeated often. I just want to find the last use of each album.
     
  6. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    Supposing the higher id for an album represents the last used, this selection should work :
    Code:
    [SIZE=2]
    SELECT MAX(id), album FROM img_manager
    GROUP BY album
    HAVING album != 'other' AND album != 'fractal'
    ORDER BY 1 DESC[/SIZE]
     
  7. Big-K

    Big-K Thread Starter

    Joined:
    Nov 22, 2003
    Messages:
    6,052
    perfect, thanks man
     
  8. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    You're welcome ! :)
     
  9. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/540374

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice