MySQL: Need help with query

Discussion in 'MySQL' started by Vaibhav, Jan 2, 2008.

  1. #1
    
    mysql> desc ticket; (ticket_id=PRI)
    +-------------------------+---------------------------------------------------+
    | Field                   | Type                                              |
    +-------------------------+---------------------------------------------------+
    | ticket_id               | bigint(20) unsigned                               |
    | ticket_subject          | char(128)                                         |
    | ticket_date             | datetime                                          |
    | ticket_status           | enum('new','bounced','resolved','dead','Escalated'|
    +-------------------------+---------------------------------------------------+
    
    PHP:
    
    mysql> desc thread;  (thread_id=PRI)
    +---------------------+-----------------------------------+
    | Field               | Type                              |
    +---------------------+-----------------------------------+
    | ticket_id           | int(11)                           |
    | thread_id           | int(10) unsigned                  |
    | thread_message_id   | char(255)                         |
    | thread_date         | datetime                          |
    | thread_subject      | char(128)                         |
    | thread_from         | char(64)                          |
    +---------------------+-----------------------------------+
    
    PHP:
    I have the above 2 tables which store emails.
    The common column is "ticket_id".

    I need to find emails (ticket_id) sent by the same sender (thread_from) and with the same subject (thread_subject) where the ticket_status=new.
    In short, identify duplicate emails.
    How can I do this ?

    It seems too complicated for me as it requires using both tables and finding duplicate / triplicate records.

    Please help.

    Thank you.
    Vai
     
    Vaibhav, Jan 2, 2008 IP
  2. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #2
    no there is a simple query to find duplicate records

    you want to delete duplicate records

    Or you want to select unique records

    Regards

    Alex
     
    kmap, Jan 2, 2008 IP
  3. RectangleMan

    RectangleMan Notable Member

    Messages:
    2,825
    Likes Received:
    132
    Best Answers:
    0
    Trophy Points:
    210
  4. Vaibhav

    Vaibhav Peon

    Messages:
    215
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I need a list of Duplicate / Triplicate records so that I can review them and then delete them if required.

    How to use UNIQUE with 2 tables.
    I do not know how to use queries where 2 tables are involved.

    A sample query on the above tables (to give the output as I have mentioned) will be of great help.

    Thx
     
    Vaibhav, Jan 3, 2008 IP
  5. Vaibhav

    Vaibhav Peon

    Messages:
    215
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    And hey, I need a list of duplicate records (not uniques).
    Thx
     
    Vaibhav, Jan 3, 2008 IP
  6. LittleJonSupportSite

    LittleJonSupportSite Peon

    Messages:
    386
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Massage this query to your needs.



    SELECT p1.fields, p2.fields
    FROM table AS p1 INNER JOIN table AS p2 ON p1.lastname = p2.lastname
    WHERE p1.id <p2.id AND

    I am using < but you can use =

    You want to use a DISTINCT on INNER join for this.
     
    LittleJonSupportSite, Jan 3, 2008 IP
  7. zybron

    zybron Peon

    Messages:
    199
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I could be totally off on this but something like the following:

    select t1.ticket_id
    from thread as t1, thread as t2, tickets
    where t1.thread_from = t2.thread_from and t1.thread_subject = t2.thread_subject and t1.ticket_id = tickets.ticket_id and t1.ticket_id <> t2.ticket_id and tickets.ticket_status = 'new'
     
    zybron, Jan 3, 2008 IP
  8. LittleJonSupportSite

    LittleJonSupportSite Peon

    Messages:
    386
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Yeah that's pretty much what I posted previously.

    You went the extra mile and used his field names where I took the lazy route and showed him the logic.

    Sometimes I do this so that people can learn on their own.

    If you always copy/paste you never learn in my opinion.
     
    LittleJonSupportSite, Jan 3, 2008 IP
  9. zybron

    zybron Peon

    Messages:
    199
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I wasn't terribly sure I had set that up right, myself. Though typically I do agree...teach a man to fish and all. :)
     
    zybron, Jan 3, 2008 IP