user6781731 user6781731 - 24 days ago 7
MySQL Question

How to select rows from two tables in laravel and return them as a single list

I have two tables,one is called patient_payments and the other is patient_billing.i now want to select all the rows from each table and then present them as a single list in my webpage.I am using laravel framework and i have used raw query as follows

$items = DB::select(DB::raw('select * from patient_payments pp,patient_billings pb'));


One table has three rows and the other five rows and i was expecting two get eight rows but the query returns 14 rows which is wrong.Any idea what is wrong with my query is much appreciated.

Thanks in advance

Answer

If both tables have the same number of columns, you can use union() method like this:

$items = DB::table('patient_payments')
    ->union(DB::table('patient_billings'))
    ->get();

Read more about Laravel Query Builder: union() here.

Hope this help.

Comments