我有这个选择:
select c.user, p.user, p.title, cm.user, cm.block, pp.foto from members cm
left join topics p on cm.member = p.id
left join profile_picture pp on pp.user = cm.user
left join cadastro c on cm.user = c.id
where cm.member = 1 and ((p.user = 10) or ((p.user != 10) and (cm.block = 0)))
group by cm.user
order by cm.block desc, cm.data desc
limit 0,100
按顺序解释:
`members` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`user` INT(11) UNSIGNED NOT NULL,
`member` INT(11) UNSIGNED NOT NULL,
`data` datetime NOT NULL,
`ip` varchar(20),
`block` tinyint(1) NOT NULL DEFAULT '0',
UNIQUE KEY (`user`, `member`),
PRIMARY KEY (`id`),
KEY `member_block` ( `member`, `block`),
FOREIGN KEY (`user`) REFERENCES cadastro (`id`),
FOREIGN KEY (`member`) REFERENCES topics (`id`)
)
and topics
`topics` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`user` INT(11) UNSIGNED NOT NULL,
`title` varchar(40),
`description` varchar(500) NOT NULL,
`foto` varchar(400),
`data` datetime NOT NULL,
`ip` varchar(20),
`privado` tinyint(1) NOT NULL DEFAULT '0',
`delete` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
FOREIGN KEY (`user`) REFERENCES cadastro (`id`)
)
我在cm.block和cm.data上尝试了一个索引,但没有成功。任何想法?