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

1my*_*myb 1 php mysql select left-join

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

表A.

  • 用户身份
  • 用户名
  • 状态
  • IntroCode

表B.

  • IntroCode
  • 用户身份

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

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

Joh*_*Woo 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
Run Code Online (Sandbox Code Playgroud)

更新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
Run Code Online (Sandbox Code Playgroud)

  • +1的速度和准确性 - 也是将@SLim链接到http://stackoverflow.com/questions/12475850/how-can-an-sql-query-return-data-from-multiple-tables的绝佳机会进一步解释这类问答:) (2认同)