简体   繁体   English

如何从我知道第二张桌子的ID的第一张桌子

[英]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 表A

  • UserID 用户身份
  • Username 用户名
  • Status 状态
  • IntroCode 简介码

Table B 表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. 我想获取表a的数据并在tblA.IntroCode = tblB.IntroCode上与表b联接,然后获取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 更新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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击提交时如何将第一个表的 id 添加到第二个表的 id_position 字段? - How to add the id of the first table to the id_position field of the second table when I click submit? 计算第二个表中与第一个表中的ID相匹配的条目 - Count entries from second Table that match id from first Table 如何获取连接表中第一个表的ID - How to get ID of the first table in a join table 使用第一个表的自动递增的ID更新第二个表 - update second table with the autoincremented id of the first table 从表中获取用户,其中group = group另一个表行,但是我有另一个表行的id - Get users from table where group=group of another table row but i have id of other table row 我想基于json-php中输出的第一张表行从第二张表获得输出结果 - I want to get output result from second table based on first table rows output in json-php 如何获取artworkrequests表的ID而不是我加入的表的ID - How do I get the Id of artworkrequests table instead of the id of the table where I join with 当我知道第二个行时,从SQL表中获取下一行的最简单方法 - Simplest way to get the next Row from an SQL table when I know the second one 如何从第二个表中获取与第三个表共同的数据列,而第三个表在第一个表上有共同点? - How to get data column from second table that have common to the third table which third table have common on the first table? 如果第一个表中不存在,我想从第二个表中获取数据 - I want fetch data from second table if not present in first table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM