简体   繁体   中英

List out column from one table, and two columns from another table

I just started learning Sql, so go easy on me. I'm trying to list out the item description for each toy, along with the customer names who bought them, as well as the dates of purchase.

Using aliases, I was able to get the name and the date_of_purchase using the following statement:

select tp.date_of_purchase, tp.name from table_a AS tp
left outer join table_b AS tt on tp.id = tt.id;

.. But I can't figure out how to get the item description from table_a as well. I need to get the name and date_of_purchase from tablea and the item_description from table_b using one sql statement. I've tried everything, but can't get it to work.

Here are the tables below:

table_a:

Column 1: id int(4) unsigned
Column 2: name varchar(100)

Column 3: quantity int(4) unsigned
Column 4: date_of_purchase date

50  John Miller         3      2013-03-16
25  Brad Cooper         1      2013-11-01
50  Alison Bradey       3      2013-11-01
50  Melissa Patterson   2      2014-02-25
75  Lisa Simpson        10     2015-08-15

table_b:

Column 1: id int(4) unsigned
Column 2: item_description varchar(100)

Column 3: quantity int(4) unsigned
Column 4: price decimal(5,2)

15  Super Nintendo          3      100.00
25  Action Figure           1      15.00
50  Gameboy                 3      65.00
20  Nintendo 64             2      150.00

Any help would really be appreciated.

item_description is located in tableb (aka tt), so all you need to do is add that field (with a tt prefix since it's in tableb) to the select list;

SELECT tp.date_of_purchase, tp.name, tt.item_description
FROM table_a AS tp
LEFT OUTER JOIN tableb AS tt 
  ON tp.id = tt.id;

An SQLfiddle to test with .

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