我有名为person
andbook
和image
and的表bookhit
。
Person 有id
, name
Book 有id
, owner_id
, info
Image 有 , 列,这id
是一个 TINYINT (在数据库中一半的行是s 和s。)顺便说一下, image 列存储了书的封面图像两个版本:大一号和缩略图。该表存储从数据库中检索图书的时间,并有一列。owner_id
url
thumbnail
0
1
bookhit
hits
所以我尝试了多次 INNER JOIN 来检索最受欢迎书籍的所有缩略图。SQL 查询如下:
SELECT `imagehit`.`hits`, `person`.`name`, `book`.`info`, `image`.`url`, `image`.`thumbnail` FROM `imagehit`
INNER JOIN `person` ON `person`.`id`=`book`.`owner_id`
INNER JOIN `image` ON `image`.`owner_id`=`book`.`id`
ORDER BY `imagehit`.`hits` DESC
WHERE `image`.`thumbnail`=1
LIMIT 10;
这不起作用,即使半行有1
s in image
。thumbnail
. 如果我更改以下行:
WHERE `image`.`thumbnail`=1
至
WHERE `image`.`thumbnail`=0
它确实有效。好吧,我走到image
桌子旁,做了一个简单的查询,如下所示:
SELECT * FROM `image` WHERE `image`.`thumbnail`=0;
并给了我存储在表中的总行数。但是当我浏览image
表时,phpMyAdmin
我看到表中1
存储了 s。:(
任何想法为什么会发生这种情况?先感谢您。
表定义:
CREATE TABLE `image` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`owner_id` int(11) NOT NULL,
`thumbnail` tinyint(1) NOT NULL,
`url` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `image_url` (`url`),
KEY `image_owner_id` (`owner_id`),
CONSTRAINT `image_ibfk_1` FOREIGN KEY (`owner_id`) REFERENCES `book` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1450 DEFAULT CHARSET=utf8
CREATE TABLE `person` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`url` varchar(60) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `person_url` (`url`),
) ENGINE=InnoDB AUTO_INCREMENT=6287 DEFAULT CHARSET=utf8
CREATE TABLE `book` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`owner_id` int(11) NOT NULL,
`book` varchar(3000) NOT NULL,
`info` varchar(3000) NOT NULL,
`url` varchar(60) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `book_url` (`url`),
KEY `book_owner_id` (`owner_id`),
CONSTRAINT `book_ibfk_1` FOREIGN KEY (`owner_id`) REFERENCES `person` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=725 DEFAULT CHARSET=utf8
CREATE TABLE `imagehit` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`owner_id` int(11) NOT NULL,
`person_id` int(11) NOT NULL,
`hits` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `imagehit_person_id` (`person_id`),
KEY `imagehit_owner_id` (`owner_id`),
KEY `hits` (`hits`),
CONSTRAINT `imagehit_ibfk_1` FOREIGN KEY (`owner_id`) REFERENCES `image` (`id`),
CONSTRAINT `imagehit_ibfk_2` FOREIGN KEY (`person_id`) REFERENCES `person` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=725 DEFAULT CHARSET=utf8
我使用 Peewee 插入数据,当我创建我设置的行时,thumbnail=True
如果图像是缩略图,就thumbnail=False
好像它不是。该列thumbnail
是 Peewee 中的字段BooleanField
。