简体   繁体   中英

How to get the first table where i know the id from second table

I need to get both table and here is the table structure

Table A

  • UserID
  • Username
  • Status
  • IntroCode

Table B

  • IntroCode
  • UserID

I want to get the table a data and join with table b on tblA.IntroCode = tblB.IntroCode, then get the username of tblB.userID. How can i do such join ?

I tried half way and stuck in the middle, please help. Thanks for reply

This is just a simple join.

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

UPDATE 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
SELECT column_name(s)
FROM TableA
LEFT JOIN TableB
ON TableA.UserID=TableB.UserID
SELECT B.userID from TableA A
LEFT JOIN TableB B on A.IntroCode=B.IntroCode
select a.*,b.IntroCode  from TableA a left join TableB b
on a.IntroCode = b.IntroCode 

you have to give the columns with same name an unique value:

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

使用此查询。

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

使用此查询

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM