Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-19-2012, 05:01 AM   PM User | #1
joshopkins
New to the CF scene

 
Join Date: Sep 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
joshopkins is an unknown quantity at this point
Update table from query

I have a query (MySQL) which pulls data from 4 tables within the same database. What I would like to do is run the query and have the results be either updated if there was a change and have new records be inserted into a separate table in another database.

Code:
    SELECT a.Created,
    a.id 'TicketID',
    GROUP_CONCAT((CASE WHEN d.CustomField = 1 THEN d.Content ELSE NULL END)) `CompanyName`,
    a.Subject,
    c.Name Queue,
    b.Name 'Owner',
    a.`Status`,
    a.LastUpdated,
    GROUP_CONCAT((CASE WHEN d.CustomField = 4 THEN d.Content ELSE NULL END)) `Location`,
    a.TimeWorked 'TimeWorked',
    GROUP_CONCAT((CASE WHEN d.CustomField = 2 THEN d.Content ELSE NULL END)) `OverRide`,
    a.Resolved
    FROM    rt.Tickets a
        INNER JOIN rt.Users b
            ON a.owner = b.id
        INNER JOIN rt.Queues c
            ON a.queue = c.id
        INNER JOIN  rt.ObjectCustomFieldValues d
            ON a.id = d.ObjectID
    GROUP BY a.id
The above query is pulls data from our ticketing system.

I was able to initally insert the data using the following:

Code:
    INSERT INTO Support (Created, TicketID, CompanyName, Subject, Queue, Owner, Status, LastUpdated, Location, Timeworked, OverRide, Resolved)
    SELECT a.Created,
    a.id 'TicketID',
    GROUP_CONCAT((CASE WHEN d.CustomField = 1 THEN d.Content ELSE NULL END)) `CompanyName`,
    a.Subject,
    c.Name Queue,
    b.Name 'Owner',
    a.`Status`,
    a.LastUpdated,
    GROUP_CONCAT((CASE WHEN d.CustomField = 4 THEN d.Content ELSE NULL END)) `Location`,
    a.TimeWorked 'TimeWorked',
    GROUP_CONCAT((CASE WHEN d.CustomField = 2 THEN d.Content ELSE NULL END)) `OverRide`,
    a.Resolved
    FROM    rt.Tickets a
        INNER JOIN rt.Users b
            ON a.owner = b.id
        INNER JOIN rt.Queues c
            ON a.queue = c.id
        INNER JOIN  rt.ObjectCustomFieldValues d
            ON a.id = d.ObjectID
    GROUP BY a.id
However when trying to update the data that is already there or adding additional new data I get errors.

Code:
    UPDATE Support
    (SELECT a.Created,
    a.id 'TicketID',
    GROUP_CONCAT((CASE WHEN d.CustomField = 1 THEN d.Content ELSE NULL END)) `CompanyName`,
    a.Subject,
    c.Name Queue,
    b.Name 'Owner',
    a.`Status`,
    a.LastUpdated,
    GROUP_CONCAT((CASE WHEN d.CustomField = 4 THEN d.Content ELSE NULL END)) `Location`,
    a.TimeWorked 'TimeWorked',
    GROUP_CONCAT((CASE WHEN d.CustomField = 2 THEN d.Content ELSE NULL END)) `OverRide`,
    a.Resolved
    FROM    rt.Tickets a
        INNER JOIN rt.Users b
            ON a.owner = b.id
        INNER JOIN rt.Queues c
            ON a.queue = c.id
        INNER JOIN  rt.ObjectCustomFieldValues d
            ON a.id = d.ObjectID
    GROUP BY a.id)
I am not sure that is the issue is on the update query.
Thanks,
joshopkins is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:11 AM.


Advertisement
Log in to turn off these ads.