0

我需要创建一个从同一个数据库中的三个表中读取的单个查询。

表:

1. cm_players
player_id
card_no
club_id
surname
name
birthdate

2. cm_clubs
club_id
prefix
name
address
place
stadium_id
telephone
fax
email
web
contact_person
president
secretary
press_officer
status
image

3. cm_registrations
player_id
surname
name
birthdate
birthplace
club_id
date_from
date_to
amateur
status

所以,我需要显示

cm_players.name, 
cm_players.surname, 
cm_clubs.name, 
cm_players.birthdate, 
cm_players.card_no,

加入 cm_players 和 cm_clubs 以获取俱乐部的名称,如果用户处于活动状态,则检查 cm_registrations .....

我试过类似的东西:

SELECT cm_players.name, cm_players.surname, cm_clubs.name, cm_players.birthdate, cm_players.card_no, cm_registrations.amateur FROM cm_players 
INNER JOIN cm_clubs on cm_players.club_id = cm_clubs.club_id, cm_registrations 
WHERE cm_players.name LIKE '%$name%' AND cm_players.surname LIKE '%$surname%' AND cm_players.player_id = cm_registrations.player_id AND cm_registrations.amateur = 0 ORDER BY cm_players.player_id ASC

但没有运气...

谁能帮我建立这个查询?

4

2 回答 2

0

cm_registrations WHERE cm_players.name LIKE '%$name%' AND cm_players.surname LIKE '%$surname%' AND cm_players.player_id = cm_registrations.player_id AND cm_registrations.amateur = 0 ORDER BY cm_players.player_id ASC 错误是在加入您使用后,比 cm_registeration 更改它并尝试修复它。

于 2013-07-20T13:42:45.943 回答
0

在这里,我们设置表别名,并通过使用该表的适当别名来获取列值,并且我们在两个表上使用内连接,即 clubs 和 registrations

SELECT cmp.name, cmp.surname, cmc.name, cmp.birthdate, cmp.card_no, cmr.amateur 
  FROM cm_players cmp 
  INNER JOIN cm_clubs cmc on cmp.club_id = cmc.club_id
  INNER JOIN cm_registrations cmr on cmp.player_id = cmr.player_id
  WHERE cmp.name LIKE '%$name%' 
  AND cmp.surname LIKE '%$surname%' 
  AND cmr.amateur = 0 
  ORDER BY cmp.player_id ASC
于 2013-07-20T13:44:03.267 回答