所以我有两个表:
socc_matches:
`mc_id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
`mc_analystid` int NOT NULL,
`mc_teamid` int NOT NULL,
`mc_date` date NOT NULL,
`mc_hometeam` varchar(60) NOT NULL,
`mc_awayteam` varchar(60) NOT NULL,
`mc_shots` tinyint(40)
socc_shots:
`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
`idmatch` int NOT NULL,
`idplayer` int,
`playername` varchar(60)
我想获取socc_shots中的所有行,其中idmatch等于表格socc_matches按日期排序的最后3 mc_id
注意:idmatch是mc_id的外键
我已尝试过此查询:
$conn = connection ();
$stmt = $conn->prepare('SELECT * FROM socc_shots INNER JOIN socc_matches ON socc_matches.mc_id = socc_shots.idmatch WHERE socc_matches.mc_id = socc_shots.idmatch ORDER BY socc_matches.mc_date desc LIMIT 3');
if (!$stmt->execute()) {
print_r($stmt->errorInfo());
}
$r = $stmt->fetchAll();
但是没有检索我想要的数据(查询错误)
我缺少什么?
提前致谢。
答案 0 :(得分:0)
这是无效的:FROM socc_matches.mc_id, socc_shots.idmatch FROM socc_matches
这不完整:WHERE socc_matches.mc_teamid = socc_shots
修正:
SELECT *
FROM socc_matches
INNER JOIN socc_shots ON socc_matches.mc_id = socc_shots.idmatch
WHERE socc_matches.mc_teamid = socc_shots.mc_teamid
ORDER BY socc_matches.mc_date desc
LIMIT 3
(将socc_shots.mc_teamid
替换为该表中调用的列。或者使用某些值,例如WHERE socc_matches.mc_teamid = 123
。)