开发者

Count on the same table

i have got a problem with my SQL Statement:

Table structure:

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

Now i want to select all the tags from one page and the frequency of this tag in the whole database. I have the following SQL statement:

SELECT title, title as t,
(SELECT 开发者_C百科COUNT(*) FROM tags WHERE title=t) as count
FROM tags WHERE page_id=42 ORDER BY count DESC, title
COLLATE NOCASE ASC;

But the error message i'm getting from SQLite3 Driver in PHP:

no such column: t

How do i have to write the SQL statement so that i get the result i want to have?

Thanks, Levu


Try this one

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;


do you mean like this...

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


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

If you want tags appear more than one

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜