指望同一张桌子

时间:2010-07-13 08:42:31

标签: sql sqlite

我的SQL语句出了问题:

表格结构:

CREATE TABLE "tags" (
  "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  "page_id" INTEGER NOT NULL,
  "title" TEXT NOT NULL
);

现在我想从一个页面中选择所有标签以及整个数据库中此标签的频率。我有以下SQL语句:

SELECT title, title as t,
(SELECT COUNT(*) FROM tags WHERE title=t) as count
FROM tags WHERE page_id=42 ORDER BY count DESC, title
COLLATE NOCASE ASC;

但是我从PHP中的SQLite3驱动程序获取的错误消息:

no such column: t

我如何编写SQL语句以便获得我想要的结果?

谢谢,Levu

3 个答案:

答案 0 :(得分:2)

试试这个

SELECT title, 
(SELECT COUNT(*) FROM tags WHERE title=t.title) as count
FROM tags t 
WHERE page_id=42 
ORDER BY count DESC, title
COLLATE NOCASE ASC;

答案 1 :(得分:1)

你的意思是这样......

SELECT title, COUNT(*) as freq
FROM tags
WHERE page_id=42
GROUP BY title
ORDER BY freq DESC, title

答案 2 :(得分:0)

select title, count(*) as frequency
from tags
where page_id = 42
group by title
order by count(*) desc

如果您希望标签出现多个

select title, count(*) as frequency
from tags
where page_id = 42
group by title
having count(*) > 1
order by count(*) desc