MySQL从两个表

时间:2017-07-18 17:48:33

标签: php mysql

我有一些问题从两个表中获得我想要的结果

table #1: history

  customer_id  |  Action
 ------------------------
  217          |  buy
 ------------------------
  218          |  sell
 ------------------------
  219          |  hold
 ------------------------

 table #2: Customers

  customer_id    |  name
 ----------------------------
  217            |  Alan
 ----------------------------
  218            |  Jan
 ----------------------------
  219            |  Rick

我现在有一个很长的查询,但实际上我想添加以匹配名称和金额。我试过这个,但它不起作用:

(SELECT action AS action FROM "history` LEFT JOIN ON " customer(customer_id=customer_id)`)

我不太熟悉查询,所以任何帮助都会受到赞赏

3 个答案:

答案 0 :(得分:3)

应该是这样的:

SELECT h.Action AS action
   FROM history h
   LEFT JOIN Customers c
   ON h.customer_id = c.customer_id

您需要指定表或创建用于关联列/数据的别名。

答案 1 :(得分:0)

是一个简单的连接

select action
from  history
left join Customers on Customers.Customer_id = history.customer_id

您可以使用

确认
select history.customer_id, Customers.Customer_id history.action , Customers.name
from  history
left join Customers on Customers.Customer_id = history.customer_id

答案 2 :(得分:0)

您可以像这样加入表格:

SELECT history.action AS Action ,Customers.name AS Name
FROM `history`
LEFT JOIN `Customers` ON history.customer_id = Customers.customer_id;