我的 SQL 数据库中有两个关联的表。一个是名为“airports”的机场列表,另一个是名为“airport_profiles”的航空公司列表。后者是我想重点关注的。我通过 airport_id 行将航空公司与机场关联起来。即身份证号码指的是机场。我当前的 PDO 查询是:
if(isset($_GET['airport'])) {
if ($_GET['airport'] == 1) {
$airportQuery = "SELECT * FROM airport_profiles";
$airport = $db->prepare($airportQuery);
$airport->execute();
}
else {
$airportQuery = "SELECT * FROM airport_profiles WHERE airport_id = :airport_id";
$airport = $db->prepare($airportQuery);
$airport->execute(['airport_id' => $_GET['airport']]);
}
$selectedAirport = $airport->fetchAll();
foreach ($selectedAirport as $profile) {
if ($profile['id'] == 1) continue;
我尝试过插入
$profile =explode(';', $airport_id);
但这根本不起作用。
如果我错了,请纠正我,但根据我的理解,为了将多个航空公司与机场关联,我需要添加一个爆炸“;”到查询,以便我的 airport_id 的分号分隔。任何建议都将受到极大的欢迎。 非常感谢。
最佳答案
查询更改为以下内容:
if(isset($_GET['airport'])) {
if ($_GET['airport'] == 1) {
$airportQuery = "SELECT * FROM airport_profiles GROUP BY airline_name";
$airport = $db->prepare($airportQuery);
$airport->execute();
}
else {
$airportQuery = "SELECT * FROM airport_profiles WHERE airport_id = :airport_id";
$airport = $db->prepare($airportQuery);
$airport->execute(['airport_id' => $_GET['airport']]);
}
$selectedAirport = $airport->fetchAll();
foreach ($selectedAirport as $profile) {
if ($profile['id'] == 1) continue;
关于PHP - 关联表的爆炸结果查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57769364/