Damien
Damien

Reputation: 95

Select record from two different table

I am really new in sql query and i have 1 question. Below are the details:

I have two tables:

user_table

user_id username    create date
1       david       2017-09-01
2       james       2017-09-14
3       damian      2017-09-24
4       craig       2017-10-02
5       lee         2017-10-05
6       cooper      2017-10-10

user_sale

sale_id user_id amount
10      1       100
11      2       200
12      3       100
13      4       500
14      5       300
15      6       100

I want to get all the username(create_date in September only) and amount that produce by each of them.

expected result:

username    amount  
david       100 
james       200 
damian      100 

THANKS.

Upvotes: 1

Views: 42

Answers (2)

Seena V P
Seena V P

Reputation: 994

SELECT usertable.username
    ,usersale.amount
FROM user_table AS usertable
INNER JOIN user_sale AS usersale ON usertable.user_id = usersale.user_id
WHERE DATE_FORMAT(created_date, "%Y-%m") = "2017-09".

This may help you

Upvotes: 1

Akshay Hegde
Akshay Hegde

Reputation: 16997

select u.username,
       s.amount 
from 
       user_table u 
join 
       user_sale  s 
on 
       u.user_id = s.user_id 
where 
       month(u.create_date) = 9

Upvotes: 1

Related Questions