我有3张桌子:
'#uses "C:\Scripts\pgGlobal.bas.txt"
Sub Main
MsgBox "Primary Width: " & GetSystemMetrics(SM_CXSCREEN) & _
" x Primary Height: " & GetSystemMetrics(SM_CYSCREEN) & vbCrLf & _
"Number of monitors: " & GetSystemMetrics(SM_CMONITORS) & vbCrLf & _
"Total Width: " & GetSystemMetrics(SM_CXVIRTUALSCREEN) & _
" x Total Height: " & GetSystemMetrics(SM_CYVIRTUALSCREEN) & vbCrLf & _
"Left Pixel: " & GetSystemMetrics(SM_XVIRTUALSCREEN) & _
" x Top Pixel: " & GetSystemMetrics(SM_XVIRTUALSCREEN)
End Sub
(用户表)
user
(带有字段作者的帖子表,用户ID为post
)
FOREIGN KEY
(表示具有friend
字段的用户与用户关系的表:FOREIGN KEY
和user_id
我有一个查询,我可以设置值来过滤这样的结果(来自Java PreparedStatement):
friend
在查询中插入值,如下所示:
SELECT post.* FROM post, friend WHERE friend.user_id = ? AND post.author = friend.friend AND post.body LIKE ? AND post.date BETWEEN ? AND ? LIMIT ?,?
我得到了5个帖子的列表。但现在,我想得到结果的数量。我试过了:
SELECT post.* FROM post, friend WHERE friend.user_id = 2 AND post.author = friend.friend AND post.body LIKE '%word%' AND post.date BETWEEN '2016-02-10 00:00:00' AND '2016-12-01 00:00:00' LIMIT 1,5;
但我得SELECT COUNT(*) FROM post, friend WHERE friend.user_id = 2 AND post.author = friend.friend AND post.body LIKE '%word%' AND post.date BETWEEN '2016-02-10 00:00:00' AND '2016-12-01 00:00:00' LIMIT 1,5;
。为什么我会这样做5?
修改
如果有帮助,我发布数据库代码和一些数据插入。
Empty set
查询:
DROP DATABASE IF EXISTS dbname;
CREATE DATABASE dbname;
USE dbname;
CREATE TABLE user (
id INT NOT NULL AUTO_INCREMENT,
username VARCHAR(20) NOT NULL UNIQUE,
name VARCHAR(70) NOT NULL,
PRIMARY KEY(id)
) ENGINE=InnoDB;
CREATE TABLE post (
id INT NOT NULL AUTO_INCREMENT,
date DATETIME NOT NULL,
title VARCHAR(100) NOT NULL,
body VARCHAR(1000) NOT NULL,
author INT NOT NULL,
PRIMARY KEY(id)
) ENGINE=InnoDB;
CREATE TABLE friend (
user_id INT NOT NULL,
friend INT NOT NULL,
PRIMARY KEY(user_id, friend)
) ENGINE=InnoDB;
ALTER TABLE post
ADD FOREIGN KEY fk_autor_post(author) REFERENCES user(id)
ON DELETE CASCADE
ON UPDATE CASCADE;
ALTER TABLE friend
ADD FOREIGN KEY fk_user_id(user_id) REFERENCES user(id)
ON DELETE CASCADE
ON UPDATE CASCADE;
ALTER TABLE friend
ADD FOREIGN KEY fk_friend_id(friend) REFERENCES user(id)
ON DELETE CASCADE
ON UPDATE CASCADE;
INSERT INTO user VALUES(null, "uname1", "User 1");
INSERT INTO user VALUES(null, "uname2", "User 2");
INSERT INTO user VALUES(null, "uname3", "User 3");
INSERT INTO user VALUES(null, "uname4", "User 4");
INSERT INTO user VALUES(null, "uname5", "User 5");
INSERT INTO friend VALUES(1,2);
INSERT INTO friend VALUES(1,3);
INSERT INTO friend VALUES(1,4);
INSERT INTO friend VALUES(1,5);
INSERT INTO friend VALUES(2,1);
INSERT INTO friend VALUES(2,5);
INSERT INTO friend VALUES(4,1);
INSERT INTO friend VALUES(2,4);
INSERT INTO friend VALUES(2,3);
INSERT INTO post values (null, '2016-01-01 10:00:00', "Title post 1", "Body 1", 1);
INSERT INTO post values (null, '2016-01-01 10:00:00', "Title post 2", "Body 2", 1);
INSERT INTO post values (null, '2016-01-01 10:20:40', "Title post 3", "Body 3", 1);
INSERT INTO post values (null, '2016-01-01 10:20:00', "Title post 4", "Body 4", 4);
INSERT INTO post values (null, '2016-02-05 10:00:00', "Title post 5", "Body 5", 5);
INSERT INTO post values (null, '2016-01-05 10:00:00', "Title post 6", "Body 6", 2);
INSERT INTO post values (null, '2016-02-01 22:00:00', "Title post 7", "Body 7", 2);
INSERT INTO post values (null, '2016-02-01 10:00:00', "Title post 8", "Body 8", 3);
INSERT INTO post values (null, '2016-01-02 10:00:00', "Title post 9", "Body 9", 5);
INSERT INTO post values (null, '2016-01-01 10:40:00', "Title post 10", "Body 10", 1);
返回:
SELECT post.* FROM post, friend WHERE friend.user_id = 2 AND post.author = friend.friend AND post.body LIKE '%%' AND post.date BETWEEN '2016-01-01 00:00:00' AND '2016-12-01 00:00:00' LIMIT 1,5;
查询:
+----+---------------------+--------------+--------+--------+
| id | date | title | body | author |
+----+---------------------+--------------+--------+--------+
| 2 | 2016-01-01 10:00:00 | Title post 2 | Body 2 | 1 |
| 3 | 2016-01-01 10:20:40 | Title post 3 | Body 3 | 1 |
| 4 | 2016-01-01 10:20:00 | Title post 4 | Body 4 | 4 |
| 5 | 2016-02-05 10:00:00 | Title post 5 | Body 5 | 5 |
| 8 | 2016-02-01 10:00:00 | Title post 8 | Body 8 | 3 |
+----+---------------------+--------------+--------+--------+
5 rows in set (0.00 sec)
返回:
SELECT COUNT(*) FROM post, friend WHERE friend.user_id = 2 AND post.author = friend.friend AND post.body LIKE '%%' AND post.date BETWEEN '2016-01-01 00:00:00' AND '2016-12-01 00:00:00' LIMIT 1,5;
使用Empty set (0.00 sec)
,COUNT(post.id)
,COUNT(post.body)
,SUM(1)
进行相同操作。
使用SUM(post.id)
时出错:
答案 0 :(得分:2)
我在自己的数据库上尝试过类似的查询,并得到与您相同的结果。我将限制从1.5更改为0.5并获得了预期的结果。我不知道这是不是你的选择。
答案 1 :(得分:1)
试试这个
SELECT COUNT(*) AS TotalCount FROM (SELECT
post.*
FROM
post,
friend
WHERE friend.user_id = 2
AND post.author = friend.friend
AND post.body LIKE '%%'
AND post.date BETWEEN '2016-01-01 00:00:00'
AND '2016-12-01 00:00:00'
LIMIT 1, 5) AS a ;