Skip to content

docs: how use straight joins #10462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,26 @@ $users = DB::table('users')
->get();
```

<a name="straight-join-clause"></a>
#### Straight Join Clause

> [!WARNING]
> Straight joins are currently supported by MySQL and MariaDB.

This method has the same signature as the `join` method

Prioritizes the users and contacts tables by first joining these two tables using a standard join, and then uses a straight join to join the orders table. Using a straight join tells the MySQL optimizer to process the tables in the specified order, thus prioritizing the join of users and contacts before joining the results with orders.

```php
use Illuminate\Support\Facades\DB;

$users = DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->straightJoin('orders', 'users.id', '=', 'orders.user_id')
->select('users.*', 'contacts.phone', 'orders.price')
->get();
```

<a name="cross-join-clause"></a>
#### Cross Join Clause

Expand Down Expand Up @@ -443,7 +463,10 @@ DB::table('users')
<a name="subquery-joins"></a>
#### Subquery Joins

You may use the `joinSub`, `leftJoinSub`, and `rightJoinSub` methods to join a query to a subquery. Each of these methods receives three arguments: the subquery, its table alias, and a closure that defines the related columns. In this example, we will retrieve a collection of users where each user record also contains the `created_at` timestamp of the user's most recently published blog post:
> [!WARNING]
> Straight joins are currently supported by MySQL and MariaDB.

You may use the `joinSub`, `leftJoinSub`, `straightJoinSub` and `rightJoinSub` methods to join a query to a subquery. Each of these methods receives three arguments: the subquery, its table alias, and a closure that defines the related columns. In this example, we will retrieve a collection of users where each user record also contains the `created_at` timestamp of the user's most recently published blog post:

```php
$latestPosts = DB::table('posts')
Expand Down