請問大家這條SQL語句有什麼錯誤(很簡單的SQL語句)?

benau 2005-01-21 11:45:33
作用:显示文章、提交人和最后回复时间
SQL: select a.title,a.username,b.adddate from table a,(select max(adddate) as adddate from table_Detail where table_Detail.title=a.title) b

以上語句被列為精妙的SQL語句,
但運行時卻出現以下錯誤:
The column prefix 'a' does not match with a table name or alias name used in the query.
(錯誤指括號裡面的a表沒有被指定)

正確的寫法應該是怎樣?或者有什麼好的辦法能夠達到以上效果,運行效率又快呢?(以上兩個資料表比較大,有30萬多條記錄)
...全文
84 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
子陌红尘 2005-01-21
  • 打赏
  • 举报
回复
select
a.title,
a.username,
b.adddate
from
table a,
(select title,max(adddate) as adddate from table_Detail group by title) b
where
a.title=b.title
daijingjie2002 2005-01-21
  • 打赏
  • 举报
回复
select a.title,a.username,b.adddate from table a inner join(select max(adddate) as adddate from table_Detail where table_Detail.title=a.title) as b on a.id=b.id
benau 2005-01-21
  • 打赏
  • 举报
回复
To libin_ftsafe(子陌红尘) :
跟樓上的 libin_ftsafe(子陌红尘)的測試環境一樣下,效能報告如下,速度快10秒:
Table 'Table'. Scan count 1, logical reads 5362, physical reads 0, read-ahead reads 0.
Table 'Table_Detail'. Scan count 1, logical reads 712, physical reads 0, read-ahead reads 0.

SQL Server Execution Times:
CPU time = 1516 ms, elapsed time = 24743 ms.
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 0 ms.
benau 2005-01-21
  • 打赏
  • 举报
回复
To daijingjie2002:
謝謝你,不過你的語句有問題哦,是不是應該這樣:
select a.title,a.username,b.adddate from table a inner join(select title,max(adddate) as adddate from table_Detail Group By title) b on a.title=b.title

Table的記錄數:150901
TableDetail的記錄數:148994

效能:
Table 'Table'. Scan count 1, logical reads 5362, physical reads 0, read-ahead reads 0.
Table 'Table_Detail'. Scan count 1, logical reads 712, physical reads 0, read-ahead reads 0.

SQL Server Execution Times:
CPU time = 1437 ms, elapsed time = 28792 ms.
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 0 ms.

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧