Santosh
Santosh

Reputation: 124

Retrieve data from two different table in a single report

I have two table Employee and Salary table, salary consists Salary of employee in a field named Salary_employee. Second one is Extra Expense, Extra expense consists records related to extra expenses of a company like electricity bills,office maintenance in a field named extra_expense.

(Their is no relationship between these two table).

Finally, I just wanted to show all the expenses of company in a report, for this i need to group both the table. what to use here join or union ??.

Upvotes: 1

Views: 3098

Answers (2)

Justin Pihony
Justin Pihony

Reputation: 67135

If there is no relationship between the two tables, then this really cannot work since you dont know where the expense is supposed to tie into. You should redesign the database if possible as this sounds impossible based on your description.

UPDATE

OK, by the look of your screenshots, I am guessing that this database only stores one companies info? And not multiple?

IF that is correct, AND if all you want to do is squish the data together into one flowing report of expenses, then I would indeed suggest a UNION. A JOIN would not give you the flow you are looking for. A UNION will just smash the two outputs together into one...which I think is what you are asking for?

SELECT ext_amount AS amount, ext_date AS date_of_trans
FROM extra_expenses
UNION
SELECT sal_cash AS amount, sal_dateof_payment AS date_of_trans
FROM employee_salary

Upvotes: 2

Armatus
Armatus

Reputation: 2191

It sounds like you don't need to use group or join. Simply query both tables separately within a script and handle them both accordingly to their structure to produce a report.

Join and union are functions which you can use to extract different information on a common thing from separate tables. E.g. if you have a user whose private details are stored in one table, but their profile information is in another table. If you want to display both private details as well as profile info, you can join the two tables by the common user name in order to combine and gather all info on the user in one query.

Upvotes: 1

Related Questions