SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: SQL help! this hould be easy?

  1. #1
    WebAmoeba mythix's Avatar
    Join Date
    Aug 2002
    Location
    here
    Posts
    578
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SQL help! this hould be easy?

    Right, I am developing a ticket system. I have two tables:

    mos_wats_ticket
    watsid int(11)
    ticketid int(11)
    ticketname varchar(25)
    vis tinyint(1)
    open tinyint(1)
    datetime timestamp(14)

    mos_wats_msg
    msgid int(11)
    ticketid int(11)
    watsid int(11)
    msg text
    datetime timestamp(14)

    And I display all tickets bt executing an SQL query that selects tickets in order of datetime - note this is the submited datetime. What i would like it to display a list of tickets ordered by the last message datetime - much as threads are ordered in this forum.

    Can I use SQL to do this or will I need to script this?
    Laws are like sausages. You have much more respect for them if you haven't actually seen how they're made.

    http://www.webamoeba.co.uk

  2. #2
    WebAmoeba mythix's Avatar
    Join Date
    Aug 2002
    Location
    here
    Posts
    578
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey come on! somebody this is an easy ine, I'm sure of it with all ur SQL brains you should be able to answer this one for me
    Laws are like sausages. You have much more respect for them if you haven't actually seen how they're made.

    http://www.webamoeba.co.uk

  3. #3
    SQL Consultant silver trophybronze trophy
    SitePoint Award Recipient r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    38,461
    Mentioned
    35 Post(s)
    Tagged
    1 Thread(s)
    what are the relationships between those two tables? how would they be joined? is there a one-to-many relationship between them, and which way (1-to-m or m-to-1) does it go?
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    WebAmoeba mythix's Avatar
    Join Date
    Aug 2002
    Location
    here
    Posts
    578
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ooo a reply, i forgot all about this post

    one ticket to many posts where mos_wats_tickets.ticketid=mos_wats_msg.ticketid
    Laws are like sausages. You have much more respect for them if you haven't actually seen how they're made.

    http://www.webamoeba.co.uk

  5. #5
    SQL Consultant silver trophybronze trophy
    SitePoint Award Recipient r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    38,461
    Mentioned
    35 Post(s)
    Tagged
    1 Thread(s)
    "display a list of tickets ordered by the last message datetime"
    Code:
    select t.watsid
         , t.ticketid
         , t.ticketname
         , t.vis
         , max(m.`datetime`) as lastpost
    open
      from mos_wats_tickets as t
    left outer
      join mos_wats_msg as m
        on t.ticketid
         = m.ticketid  
    group
        by t.watsid
         , t.ticketid
         , t.ticketname
         , t.vis
    order
        by lastpost desc
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •