开发者

MySQL Query for ticketing system in custom back-end

I'm trying to add a ticketing system to a custom back-end that powers the rest of the site. I'm having a bit of trouble though in ordering the tickets (once a reply has been made). I am fairly new to MySQL and not quite sure how to proceed.

At the moment I have the following:

SELECT * FROM tbl_tickets ORDER BY ti_date_last_reply DESC, ti_date_raised DESC LIMIT 0, 20

The above works fine except that if a new ticket is raised the existing replies appear above it.

I have 4 ticket status stored in ti_status - Open (a new ticket), Answered (admin has replied to ticket), Customer-Reply (customer has replied or added more info to ticket), and Closed (ticket is closed).

What I would like is for the MySQL to order them by ti_date_raised if the ticket status is Open (oldest first), then add Customer-Reply which is foun开发者_如何学Cd in ti_date_last_reply to slot into the correct time frame. Leaving Answered and Closed to appear below these queries (oldest to youngest) - but Answered goes above Closed

Example (I used date/time here, but I use Unix time in the scripts):

Date Raised      |   Date Replied   | Status  
----------------------------------------------------------  
2011-01-30-20-30 | -                | Open  
2011-01-31-01-00 | 2011-02-01-09-30 | Customer-Reply  
2011-02-01-10-30 | -                | Open  
2011-02-01-20-00 | 2011-02-01-20-30 | Answered  
2011-02-01-21-00 | 2011-02-01-20-35 | Closed

Is such a thing possible?


SELECT * 
FROM tbl_tickets
ORDER BY FIND_IN_SET(ti_status,'Open,Customer-Reply,Answered,Closed'),
    IF(ti_status = 'Customer-Reply', ti_date_last_reply, ti_date_raised) DESC
LIMIT 0, 20

Changing ti_status to a status ID number and storing the statuses (and sort number) in a second table will improve performance by a order of magnitude.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜