我有一个这样的表orders-

id  | bookId
------------
1      3
2      2
3      1


而这个books-

bookId  |  book
---------------
  1       bookA
  2       bookB
  3       bookC


当我这样做时,他们能以何种方式获取book下的bookId

select * from orders where id = '1'

这样的结果将是-

id  | bookId
------------
1      bookC         <------- bookC instead of 3

最佳答案

您需要将bookid表中orders列上的表JOIN映射到bookid表中的books列:

select o.id, b.book as bookId
from orders o
inner join books b
  on o.bookid = b.bookid
where o.id = 1;

关于mysql - 从多个表中获取结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17374126/

10-10 05:46