tags:

views:

3366

answers:

3
+1  A: 

Assuming at least one ticket, you could do this instead:

SELECT MAX(g.ticket) FROM ticket_custom g WHERE g.ticket=id

You can convert that to a JOIN or nested SELECT etc.

so change id=c.ticket id=MAX(c.ticket)

but check your WHERE & LEFT JOIN there's some strange stuff in there.

Steven
I agree, you should be able to get rid of your subquery completely
Nathan Koop
+1  A: 

This appears to be more of a design issue than a query issue. While it is possible to return only a single record from the ticket_custom table there is not a way to determine which record that will be. In you sample table ticket 1 has a value of "Ready for..." and "Verified" but nothing that shows which happened first. Logically, the "Ready for..." happened first.

The easiest way for you to fix this issue would be to add a timestamp or incrementing ID field. Then your subquery could select the most recent entry (latest timestamp or highest ID).

Jeremy
A: 

Don't make your database schema dynamic (i.e. "table-driven"). It isn't a sensible design pattern (I know from experience). It saves only a little effort upfront in creating additional attributes, but incurs additional overhead thereafter, both in writing queries and in the database processing required to execute them.

Just add a state id column to the ticket table and update it when the state changes. Create a reference table to store the state names. Then your queries become quite simple, and requires less effort for database engine to do an indexed join to a small reference table.

Indolent Code Monkey
I agree. I don't have access to the SQL query doing the insert. I only am able to read data from the tables.
Brad8118
Jeremy's right. In your 3-row sample of the ticket_custom table, what is the current state for ticket 1? Obviously Verified, but there's no **elegant** way for a SQL statement to know that Verified is higher than Ready for Verification -- there's a very ugly solution involving CASE-WHEN statements, but you don't want to go there. In short, the ticket_custom table is missing some necessary info. Adding a timestamp or auto-id like Jeremy says will be a step in the right direction.
Indolent Code Monkey