MySQL通过ID连接2个表

MySQL通过ID连接2个表,mysql,join,Mysql,Join,在我的桌上团队中,我有唯一的ID player_1、player_2、player_3、player_4、player_5,在我的桌上球员中,这些ID指的是球员的名字。例如,在搜索脚本案例中,按名称搜索: The user write "foo" but "foo" is ID 20. "teams" ID 1 => player_1 ID = 20 "players" player_id = 20 > name = foo 我的问题是: $sql = 'SELECT * FRO

在我的桌上团队中,我有唯一的ID player_1、player_2、player_3、player_4、player_5,在我的桌上球员中,这些ID指的是球员的名字。例如,在搜索脚本案例中,按名称搜索:

The user write "foo" but "foo" is ID 20.

"teams" ID 1 => player_1 ID = 20
"players" player_id = 20 > name = foo
我的问题是:

$sql = 'SELECT * FROM teams LEFT JOIN players ON players.name LIKE :word';
但它没有像我想的那样起作用

尝试下面的查询

select * from teams as tm,players as py where tm.player_1=py.player_id and py.name LIKE 'foo%'

为什么您需要加入团队表?您只能使用“玩家”表按名称搜索。

加入id=player\u id并按名称搜索:

select * from teams join players on player_id = ID where name like 'Adam%';
+-----------+---------+----+---------+
| player_id | team_id | ID | name    |
+-----------+---------+----+---------+
|         1 |       1 |  1 | Adam G. |
|         3 |       2 |  3 | Adam S. |
+-----------+---------+----+---------+
如果在两个表中使用相同的字段名,则可以基于列进行联接:

select * from teams join players using (player_id) where name like '%S.';
+-----------+---------+---------+
| player_id | team_id | name    |
+-----------+---------+---------+
|         3 |       2 | Adam S. |
|         5 |       1 | Jim S.  |
+-----------+---------+---------+
2 rows in set (0.01 sec)

您需要一个条件表达式。见:。。。p、 您的LIKE表达式看起来也连接在一起,check-ON应该用于链接两个表,您应该给出用于连接表的列,其中条件是您在比较列和字符串时需要的条件