简体   繁体   English

MySQL PHP ID在一个表中而不是另一个表中

[英]MySQL PHP ID in one table and not another

| Fixture_ID | League_ID | Home_Team | Away_Team 
|          1 |         1 |         1 |         2 
|          2 |         1 |         2 |         3 
|          3 |         1 |         3 |         1 

| Result_ID | Fixture_ID | Home_Goals | Away_Goals 
|         1 |          1 |          2 |          0

| Team_ID | Team_Name   |
|       1 | Team A  
|       2 | Team B      
|       3 | Team C 

How do I join the tables to show only the fixtures that haven't had results inputed but output the actual team names (Team A v Team B) when showing the fixture (in a drop down list)? 我如何加入表格以仅显示未输入结果的灯具,但在显示灯具时(在下拉列表中)输出实际的团队名称(Team A v Team B)?

The following code works for outputting all fixtures: 以下代码适用于输出所有灯具:

echo '<td> <select name ="fixture_id">';    

// TRY TO SHOW FIXTURES WITH NO RESULTS
$stmt = $pdo->prepare('SELECT  f.*, t1.Team_Name AS Home, t2.Team_Name AS Away
                        FROM Fixture        f
                        INNER JOIN Team     t1 ON f.Home_Team = t1.Team_ID
                        INNER JOIN Team     t2 ON f.Away_Team = t2.Team_ID');


$stmt->execute();
foreach ($stmt as $row) {
    echo '<option>' . $row['Home'] . ' v ' .  $row['Away'] . '</option>';
}  

?>  

Your SQL should look like this: 您的SQL应如下所示:

SELECT  f.*, t1.Team_Name AS Home, t2.Team_Name AS Away
FROM Fixture        f
INNER JOIN Team     t1 ON f.Home_Team = t1.Team_ID
INNER JOIN Team     t2 ON f.Away_Team = t2.Team_ID
LEFT JOIN Result    r ON f.Fixture_ID = r.Fixture_ID
WHERE r.id IS NULL;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在MySQL php中将ID从一个表插入另一个表 - Insert ID from one table to another in MySQL php 如何在php中插入mysql的自动递增字段ID,并将一个表的ID更新为另一个表? - How to insert the auto incremented field id of mysql in php and update the id of one table to another? PHP / MySQL的-从一个表中选择ID,除了在另一个表中的ID - php / mysql - select id from one table excepting ids which are in another table PHP MySQL从一个表中选择ID,从另一个表中选择信息 - PHP MySQL Select ID from one table and information from another table PHP MySQL根据另一张表中的ID在一行中查询两个用户名 - Php mysql Query two usernames in one row based on id from another table PHP MYSQL从另一个表插入一个表 - PHP MYSQL Insert Into one table from another PHP和MySQL-如果内容不在一个表中,请检查另一个表 - PHP & MySQL - If content is not in one table, check another MySQL表联接:一个记录,两个字段,另一表中的id - MySQL Table Join: One record, two fields, id in another table PHP从表中获取MYSQL id,并在另一个表中获取foreach - PHP get MYSQL id from on table and foreach inside another table PHP:从一个 mysql 表中获取最后一行的 id 号,并使用它来更新另一个表的第一行中的字段 - PHP: Taking the last row's id number from one mysql table and using it to update a field in the first row of another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM