如何使用连接查询从同一个表中搜索多个名称

时间:2022-09-19 18:54:03

I have 2 tables. In first table

我有2张桌子。在第一张表中

id  book_name   date
--------------------------
2    php       12-10-2015
3   java       12-10-2015
---------------------------

in the second table

在第二个表中

id  book_1  book_2   
-------------------
12   2       3
------------------

now what will be the SQL query for viewing like this when search in SQL

现在,当在SQL中搜索时,用于查看的SQL查询是什么

id  book_1  book_2
---------------------
12   php    java
---------------------

1 个解决方案

#1


0  

You are looking for JOIN.

你正在寻找加入。

You JOIN table2 with table1 twice, using ALIAS to difference between them.

你用table1两次加入table2两次,使用ALIAS来区分它们。

SELECT T2.id, A.book_name, B.book_name
FROM Table2 T2
JOIN Table1 A
  ON T2.book_1   = A.id  
JOIN Table1 B
  ON T2.book_2   = B.id  

#1


0  

You are looking for JOIN.

你正在寻找加入。

You JOIN table2 with table1 twice, using ALIAS to difference between them.

你用table1两次加入table2两次,使用ALIAS来区分它们。

SELECT T2.id, A.book_name, B.book_name
FROM Table2 T2
JOIN Table1 A
  ON T2.book_1   = A.id  
JOIN Table1 B
  ON T2.book_2   = B.id