我有2张桌子。
表1
+----+------+
| Id | Name |
+----+------+
| | |
+----+------+
表2
+-----+-----------+------+-------+---------+
| Id | Table1_ID | Name | Value | Created |
+-----+-----------+------+-------+---------+
| | | | | |
+-----+-----------+------+-------+---------+
当我运行SELECT * FROM Table2
时,我希望将Table1_ID
替换为来自name
的该项ID
的{{1}},而不是Table 1
{1}}。我怎么能这样做?
答案 0 :(得分:2)
用户内部联接,像这样
SELECT
T2.Id
T1_Name = T1.Name ,--Table1_ID
T2_Name = T2.Name
T2.Value
T2.Created
FROM Table1 T1
INNER JOIN Table2 T2
ON T1.ID = T2.Table1_ID
答案 1 :(得分:1)
您可以使用INNER JOIN
。
INNER JOIN语法1
SELECT *
FROM table1
INNER JOIN table2 ON table1.id = table2.fk_id
INNER JOIN语法2
SELECT *
FROM table1
INNER JOIN table2
WHERE table1.id = table2.fk_id
SELECT Table2.Id, Table2.Name, Table1.Name, Table2.Value, Table2.Created
FROM Table2
INNER JOIN Table1 ON Table1.ID = Table2.Table1_ID
推荐读物
http://sql.sh/cours/jointures/inner-join
https://www.w3schools.com/sql/sql_join_inner
https://www.tutorialspoint.com/sql/sql-inner-joins