Faster SQL-query

Faster SQL-query, huge performance improvement on installations with thousands of tickets. We use osTicket and have about 25 000 tickets. Increasing by 1000-1500 per month. Our employees were complaining about the speed of the system, so I decided to look into that. I think I found the sinner SQL-query, and rewrote it to be a lot faster.