gpt4 book ai didi

php - 使用 3 select 向先前的查询添加约束

转载 作者:行者123 更新时间:2023-11-29 10:15:35 24 4
gpt4 key购买 nike

我有这个查询:

$stmt = $conn->prepare(
'SELECT *
FROM table_shots
WHERE idmatch in (
select *
from (
SELECT mc_id
FROM table_matches
WHERE mc_hometeam = '. $idTeam .'
OR mc_awayteam = '. $idTeam .'
ORDER BY mc_date desc
LIMIT '. $num .'
) temp_tab
)'
);

现在我需要添加另一个条件 table_shots

table_shots 也有这个字段:teamtype。该字段可以有两个值:home_teamaway_team

现在(在我看来)我们需要的是找到一种方法从第三个 SELECT 中了解 $idTeam 是否在 mc_hometeam 中> 或 mc_awayteam(来自 table_matches)以添加新条件。

新条件将添加到表table_shots(理论上),如下所示:AND teamtype = home_team(如果 $idTeam code> 位于 table_matchesmc_hometeam 中)或 AND teamtype =away_team (如果 $idTeam 位于 table_matchesmc_awayteam 中)。

有什么建议吗?

我知道这很难,让我惊讶;)

最佳答案

为什么不使用简单的联接而不是多个选择?

$stmt = $conn->prepare('SELECT ts.*     FROM table_shots AS ts     JOIN table_matches AS tm ON ts.idmatch = tm.mc_id    WHERE (mc_hometeam = '. $idTeam .' AND teamtype = 'home_team') OR (mc_awayteam = '. $idTeam .' AND teamtype = 'away_team')    ORDER BY mc_date desc     LIMIT '. $num);

此外,我建议使用 PDO 和参数,而不是将 $idTeam var 连接到查询字符串。

$stmt = $conn->prepare('SELECT ts.*     FROM table_shots AS ts     JOIN table_matches AS tm ON ts.idmatch = tm.mc_id    WHERE (mc_hometeam = :idTeam AND teamtype = 'home_team') OR (mc_awayteam = :idTeam AND teamtype = 'away_team')    ORDER BY mc_date desc     LIMIT '. $num);$stmt->execute([':idTeam' => $idTeam]);

编辑:你是对的,LIMIT 放错了地方。乍一看,将两者结合起来应该可行。

编辑2:下面的第一个查询有效,但在第二个查询中,我将 min() 放在了错误的位置。第三个也有同样的问题。修复了它们。

$stmt = $conn->prepare('SELECT ts.*     FROM table_shots AS ts     JOIN table_matches AS tm ON ts.idmatch = tm.mc_id    WHERE ((mc_hometeam = :idTeam AND teamtype = 'home_team') OR (mc_awayteam = :idTeam AND teamtype = 'away_team'))    AND idmatch in (                        select *                         from (                            SELECT mc_id                             FROM table_matches                             WHERE mc_hometeam = :idTeam                             OR mc_awayteam = :idTeam                             ORDER BY mc_date desc                             LIMIT '. $num .'                            ) temp_tab                    ));$stmt->execute([':idTeam' => $idTeam]);

或者这种方法也可以工作,我不确定哪个更好。

$stmt = $conn->prepare('SELECT ts.*     FROM table_shots AS ts     JOIN table_matches AS tm ON ts.idmatch = tm.mc_id    WHERE ((mc_hometeam = :idTeam AND teamtype = 'home_team') OR (mc_awayteam = :idTeam AND teamtype = 'away_team'))    AND mc_date >= (                        SELECT min(mc_date)                        FROM (                            SELECT mc_date                            FROM table_matches                             WHERE mc_hometeam = :idTeam                             OR mc_awayteam = :idTeam                             ORDER BY mc_date desc                             LIMIT '. $num .'                            ) temp_tab                    ));$stmt->execute([':idTeam' => $idTeam]);

或者,当然您可以在单独的查询中获取 minDate。我认为这更具可读性。

$stmt = $conn->prepare('SELECT min(mc_date)                        FROM (                            SELECT mc_date                            FROM table_matches                             WHERE mc_hometeam = :idTeam                             OR mc_awayteam = :idTeam                             ORDER BY mc_date desc                             LIMIT '. $num .'                            ) temp_tab);$stmt->execute([':idTeam' => $idTeam]);$result = $stmt->fetch(\PDO::FETCH_ASSOC);$stmt = $conn->prepare('SELECT ts.*     FROM table_shots AS ts     JOIN table_matches AS tm ON ts.idmatch = tm.mc_id    WHERE ((mc_hometeam = :idTeam AND teamtype = 'home_team') OR (mc_awayteam = :idTeam AND teamtype = 'away_team'))    AND mc_date >= :minDate');$stmt->execute([':idTeam' => $idTeam, ':minDate' => $result['minDate']]);

关于php - 使用 3 select 向先前的查询添加约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50164359/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com