简体   繁体   English

MySQL从两个表中获取数据

[英]MySQL getting data from two tables

Im having some issues getting the result I want from two tables 我在从两个表中获取我想要的结果时遇到问题

table #1: history

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

 table #2: Customers

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

I have a really long query now, but essentially I want to add to match the name with the amount. 我现在有一个很长的查询,但本质上我想添加以使名称与金额匹配。 I tried this but it didn't work: 我试过了,但是没有用:

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

I'm not really familiar with doing queries so any help would be appreciated 我对查询不是很熟悉,因此不胜感激

It should be this: 应该是这样的:

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

You either need to specify the tables or create an alias with which to associate columns/data. 您要么需要指定表,要么创建一个别名以与列/数据相关联。

Is a simple join 是一个简单的连接

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

and you can confirm using 你可以确认使用

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

You can JOIN tables like this: 您可以像这样联接表:

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM