0

Consider 2 Table

  1. Table1
    (id,name,phoneNumber,state,city,country........)
  2. Table2
    (id,profession,personId)

I want to show all Column entries (excluding Table2.id)

The Traditional Query which we are using i.e.

SQL:

SELECT Table1.id, Table1.name, Table1.phoneNumber, Table1.state, Table1.city, Table1.country,
........, Table2.profession FROM Table1 join Table2 on Table2.personId=Table1.id

Laravel :

Table1::join('Table2', 'Table2.personId', 'Table1.id')
    ->Select('Table1.id', 'Table1.name', 'Table1.phoneNumber', 'Table1.state', 'Table1.city', 'Table1.country', ........, 'Table2.profession')
    ->get();

As we know we have to write the Column name in the query. Is there any query in which we can minimize the effort of writing that means we have to write only that Column which we don't want to show?

Rwd
  • 34,180
  • 6
  • 64
  • 78
Biki mallik
  • 142
  • 1
  • 9

2 Answers2

1

You can use the all character (*) for this:

SELECT Table1.*, Table2.profession FROM Table1 join Table2 on Table2.personId=Table1.id

or with Laravel's query builder:

DB::table('Table1')
    ->select('Table1.*', 'Table2.profession')
    ->join('Table2', 'Table1.id', 'Table2.personId')
    ->get();
Rwd
  • 34,180
  • 6
  • 64
  • 78
0

I Got also an similar Answer(If there is any Syntax error bymistakely please ignore ) For Laravel:

DB::table('Table1')
    ->join(DB:Raw('(SELECT personId,profession from Table2) as TempTable'), 'Table1.id', 'TempTable.personId')
    ->get();

For SQL:

Select * from Table1 Join (SELECT personId,profession from Table2 ) as TempTable on TempTable.personId=Table1.id
Biki mallik
  • 142
  • 1
  • 9