hello pls i need help on my school project. I have tables try1 and try2, i want to join the two showing the income and expenditure sorted with the date accordingly from the try1 and try2
try1 +------+-------+--------+--------+ | id | date | name |expensis| +------+-------+--------+--------+ | 1 |17/11/1| shoe | 200 | +------+-------+--------+--------+ | 2 |17/11/2| books | 300 | +------+-------+--------+--------+ | 3 |17/11/2| T.fair | 400 | +------+-------+--------+--------+
try2 +------+-------+--------+--------+ | id | date | name | source | +------+-------+--------+--------+ | 1 |17/11/1| Dad | 1000 | +------+-------+--------+--------+ | 2 |17/11/2| uncle | | +------+-------+--------+--------+ | 3 |17/11/4| Bro | 150 | +------+-------+--------+--------+
this is how i want my table to look like
RESULT +-------+--------+--------+ | date |expensis| source | +-------+--------+--------+ |17/11/1| 200 | 1000 | +-------+--------+--------+ |17/11/2| 300 | | +-------+--------+--------+ |17/11/2| 400 | | +-------+--------+--------+ |17/11/4| | 150 | +-------+--------+--------+
i tried many codes and it didnt work out this is the code somewhat close to what i want
SELECT u.date, b.amount, h.amount FROM (SELECT Date FROM try1 UNION SELECT Date FROM try2) u RIGHT JOIN try1 b ON u.Date=b.Date RIGHT JOIN try2 h ON u.Date=h.Date
EDIT: ddl and sample data
CREATE TABLE try1
( id int(2) AUTO_INCREMENT
, date date
, name varchar(10)
, expencis double(10,0)
, PRIMARY KEY(id) );
CREATE TABLE try2
( id int(2) AUTO_INCREMENT
, date date
, name varchar(10)
, sourcee int(10)
, PRIMARY KEY(id) );
INSERT into try1 (date,name,expencis)
VALUES ('2017-11-1','shoe',200)
, ('2017-11-2','books',300)
, ('2017-11-2','t.fair',400);
INSERT into try2 (date,name,sourcee)
VALUES ('2017-11-1','dad',1000)
, ('2017-11-2','uncle',null)
, ('2017-11-4','bro',null);
create table
andinsert
commands for your sample data, the join criteria, and also provide the query(s) you've tried so far; you need to put some (more) effort into your question and show us what you've tried so far ... we're not here to do your homework for youJOIN
s. In a right join, the table on the right side of the join will be included regardless of whether or not there's a match; the table on the left side will be ignored except where it matches the table on the right. I believe you'd want the date (the left table) regardless of whether or nottry1
ortry2
has a match (at least one will, of course).RIGHT JOIN
s withLEFT JOIN
s, as I suggested?