根据COUNT条件创建表

时间:2014-02-23 12:23:23

标签: sql sqlite

我的数据库中有两个表。表up_to_date为每条记录指示特定页面(commentNumber)应包含的评论数(url)。表comment包含我在db中具有相应url的实际注释。

CREATE TABLE up_to_date
 (id INTEGER PRIMARY KEY,
 url TEXT NOT NULL,
 commentNumber INTEGER)

CREATE TABLE comment 
 (id INTEGER PRIMARY KEY,
 commentMessage TEXT,
 url TEXT NOT NULL)

我想创建一个表url_to_update,其中包含我需要更新的网页网址:特定网页comment中的记录数小于up_to_date.commentNumber中指示的记录数对于同一页面。

这样的东西
CREATE TABLE url_to_update AS
 (SELECT * FROM up_to_date 
WHERE up_to_date.commentNumber > COUNT(comment.url = up_to_date.url))

1 个答案:

答案 0 :(得分:2)

<{1>}在SQLite中,您可以使用此语法create table Table_2 as select * from Table_1创建表格:

CREATE TABLE url_to_update AS
SELECT * FROM up_to_date 
WHERE up_to_date.commentNumber > (SELECT COUNT(comment.url) FROM comment 
WHERE comment.url= up_to_date.url);

Here is a sample which you can see