0

Want select all columns from three table without common column

  $data = DB::table('users')
         ->join('country.*','users.*')
         ->join('city.*', 'country.*')
         ->where('users.id', '=', $id)
         ->get();

Above query showing

Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax error

jarlh
  • 42,561
  • 8
  • 45
  • 63
koushiki
  • 9
  • 4

1 Answers1

0

I personally never use Joins in laravel because you can easily just look up in various tables using the Models given an ID or a key; relationships are pretty easy and straightforward that way. But if you absolutely need to use Join, it seems like your syntax is missing a few parameters, like What is it joining based on? Just by skimming the laravel documentation, I found this example:

$users = DB::table('users')
            ->join('contacts', 'users.id', '=', 'contacts.user_id')
            ->join('orders', 'users.id', '=', 'orders.user_id')
            ->select('users.*', 'contacts.phone', 'orders.price')
            ->get();
  • i dont have a common columns for tables then how can i compare, then i want select whole table columns not particular columns – koushiki May 25 '21 at 09:22
  • Then I think what you're looking for is a UNION rather than a JOIN. Take a look at the following links, they should help: https://stackoverflow.com/questions/41756404/laravel-eloquent-union-query https://laravel.com/docs/8.x/queries#unions It is essentially running two select queries then combining the results, which seems to be what you want since there's no common column to join the tables. –  May 25 '21 at 13:57