如何从第二个表中获取我知道id的第一个表

时间:2012-09-26 09:08:18

标签: php mysql select left-join

我需要得到两个表,这里是表结构

表A

  • 用户ID
  • 用户名
  • 状态
  • IntroCode

表B

  • IntroCode
  • 用户ID

我想在tblA.IntroCode = tblB.IntroCode上获取表格数据并与表b连接,然后获取tblB.userID的用户名。我怎么能这样加入?

我尝试了一半,卡在中间,请帮忙。谢谢你的回复

7 个答案:

答案 0 :(得分:6)

这只是一个简单的连接。

SELECT  a.*, b.*    -- select your desired columns here
FROM    tableA a
        INNER JOIN tableB b
            ON a.IntroCode = b.IntroCode
WHERE   b.userid = valueHere

更新1

SELECT  a.UserID, 
        a.`Username` OrigUserName,
        a.`Status`,
        c.`Username` IntroUserName
FROM    tableA a
        INNER JOIN tableB b
            ON a.IntroCode = b.IntroCode
        INNER JOIN tableA c
            ON b.userID = c.userID
-- WHERE b.UserID = valueHere       -- extra condition here

答案 1 :(得分:3)

SELECT column_name(s)
FROM TableA
LEFT JOIN TableB
ON TableA.UserID=TableB.UserID

答案 2 :(得分:2)

SELECT B.userID from TableA A
LEFT JOIN TableB B on A.IntroCode=B.IntroCode

答案 3 :(得分:2)

select a.*,b.IntroCode  from TableA a left join TableB b
on a.IntroCode = b.IntroCode 

答案 4 :(得分:2)

您必须为具有相同名称的列提供唯一值:

SELECT  a.UserID as uid_a, b.UserID as uid_b
FROM    tableA a
INNER JOIN tableB b ON a.IntroCode = b.IntroCode
WHERE   b.UserID = 1

答案 5 :(得分:2)

使用此查询。

 SELECT TableA.Username FROM TableA JOIN TableB ON (TableA.IntroCode = TableB.IntroCode);

答案 6 :(得分:1)

使用此查询

SELECT  *  FROM tblA INNER JOIN tblB ON tblA.IntroCode = tblB.IntroCode where tblB.userid = value