MYSQL查询问题

时间:2011-08-22 19:31:23

标签: php mysql sql

我有一个MYSQL查询,基本上我有两个表,一个叫做Leagues,另一个叫做Members,我需要从member_id列的成员表中检索名称,用户名,密码=到的member_id列联赛表,联赛表中的league_id ='5'。

我真的不知道如何编写这段代码以便它可以工作? 任何帮助都会很棒。

6 个答案:

答案 0 :(得分:1)

基本联接是必要的:

select name, username, password from Leagues, Members 
       where Leagues.member_id = Members.member_id and league_id = 5

或者,您可以使用sql常用的简写方法:

select name, username, password from Leagues lg, Members m 
       where lg.member_id = m.member_id and lg.league_id = 5

请注意,仅当列名在连接表之间不唯一时才需要此“速记”。

祝你好运! -Knunk

答案 1 :(得分:0)

select m.username, m.password, m.name 
from Leagues l 
inner join Members m on (m.member_id=l.member_id)
where l.league_id=5

此查询将返回您的结果。

答案 2 :(得分:0)

SELECT
  name,
  username, 
  password
FROM Members JOIN Leagues ON Members.member_id = Leagues.member_id
WHERE Leagues.league_id = 5

答案 3 :(得分:0)

SELECT members.name, members.username, members.password 
FROM  members
INNER JOIN Leagues  ON(Leagues.member_id = members.member_id)
WHERE Leagues.league_id = 5

答案 4 :(得分:0)

select
  m.name, m.username, m.password 
from 
  members m, 
  leagues l
where
  m.member_id = l.member_id and
  league_id = 5;

答案 5 :(得分:0)

SELECT DISTINCT M.name, M.username, M.password
FROM Leagues AS L
INNER JOIN Members AS M
ON L.member_id = M.member_id
WHERE league_id = 5