简体   繁体   中英

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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