我有2張表,ticket和ticket_custom。
下面是如何設置表格。 SQL - 嵌套select語句?
我有一個Web界面,我可以在其中更改狀態(表中的值ticket_custom)Web界面添加一個新條目而不是更新原始條目。
ticket name value
1 state Ready for Final Verification
2 state Ready for Final Verification
1 state Verified
最後一行添加
所以我需要修改查詢。
SELECT p.value AS __color__,
id AS ticket, summary, component, version, c.value AS state, milestone, t.type AS type,
owner, status,
time AS created,
changetime AS _changetime, description AS _description,
reporter AS _reporter
FROM ticket t, ticket_custom c
LEFT JOIN enum p ON p.name = t.priority AND p.type = 'priority' AND id = c.ticket
WHERE status <> 'closed' AND id = c.ticket
ORDER BY CAST(p.value AS int), milestone, t.type, time, c.ticket
該查詢現在返回兩個條目。我試圖在where子句中添加一個嵌套的select。
SELECT g.ticket
FROM ticket_custom g
WHERE g.ticket = id
ORDER BY g.ticket DESC LIMIT 1
所以 -
SELECT p.value AS __color__,
id AS ticket, summary, component, version, c.value AS state, milestone, t.type AS type,
owner, status,
time AS created,
changetime AS _changetime, description AS _description,
reporter AS _reporter
FROM ticket t, ticket_custom c
LEFT JOIN enum p ON p.name = t.priority AND p.type = 'priority' AND id = c.ticket
WHERE status <> 'closed' AND id = c.ticket and (
SELECT g.ticket
FROM ticket_custom g
WHERE g.ticket = id
ORDER BY g.ticket DESC LIMIT 1)
ORDER BY CAST(p.value AS int), milestone, t.type, time, c.ticket
顯然,我做錯了什麼。
我同意,你應該能夠完全擺脫你的子查詢 – 2009-04-24 19:38:15