除非我打错字
SELECT
t3.*
FROM
t3
LEFT JOIN
t2 ON t3.cid = t2.cid
LEFT JOIN
t1 ON t2.uid = t1.uid
WHERE
t1.gid = 1;
带表:
CREATE TABLE IF NOT EXISTS `t1` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
`gid` int(11) NOT NULL,
PRIMARY KEY (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
INSERT INTO `t1` (`uid`, `gid`) VALUES
(1, 0),
(2, 1),
(3, 1),
(4, 2);
CREATE TABLE IF NOT EXISTS `t2` (
`cid` int(11) NOT NULL AUTO_INCREMENT,
`uid` int(11) NOT NULL,
PRIMARY KEY (`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
INSERT INTO `t2` (`cid`, `uid`) VALUES
(1, 2),
(2, 3),
(3, 4),
(4, 5);
CREATE TABLE IF NOT EXISTS `t3` (
`lid` int(11) NOT NULL AUTO_INCREMENT,
`cid` int(11) NOT NULL,
PRIMARY KEY (`lid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
INSERT INTO `t3` (`lid`, `cid`) VALUES
(1, 2),
(2, 2),
(3, 3),
(4, 1);