Skip to content
This repository was archived by the owner on Nov 26, 2022. It is now read-only.

Commit f2ac990

Browse files
author
Daniel Opitz
committed
Removed Cross Join Clause
1 parent c8d8b48 commit f2ac990

File tree

3 files changed

+8
-92
lines changed

3 files changed

+8
-92
lines changed

docs/readme.md

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -230,22 +230,14 @@ LEFT JOIN `posts` ON `users`.`id` = `posts`.`user_id`;
230230

231231
#### Cross Join Clause
232232

233-
To perform a "cross join" use the crossJoin method with the name of the table you wish to cross join to.
234-
Cross joins generate a cartesian product between the first table and the joined table:
233+
From the [MySQL JOIN](https://dev.mysql.com/doc/refman/5.7/en/nested-join-optimization.html) docs:
235234

236-
```php
237-
$users = $this->select()
238-
->from('sizes')
239-
->crossJoin('posts', 'users.id', '=', 'posts.user_id')
240-
->execute()
241-
->fetchAll();
242-
```
235+
> In MySQL, CROSS JOIN is syntactically equivalent to INNER JOIN; they can replace each other.
236+
> In standard SQL, they are not equivalent. INNER JOIN is used with an ON clause; CROSS JOIN is used otherwise.
243237
244-
```sql
245-
SELECT *
246-
FROM `users`
247-
CROSS JOIN `posts` ON `users`.`id` = `posts`.`user_id`;
248-
```
238+
In MySQL Inner Join and Cross Join yielding the same result.
239+
240+
Please use the [join](#inner-join-clause) method.
249241

250242
#### Advanced Join Clauses
251243

@@ -262,7 +254,8 @@ $users = $this->select()
262254
```
263255

264256
```sql
265-
SELECT `id` FROM `users` AS `u` INNER JOIN `posts` AS `p` ON (p.user_id=u.id AND u.enabled=1 OR p.published IS NULL);
257+
SELECT `id` FROM `users` AS `u`
258+
INNER JOIN `posts` AS `p` ON (p.user_id=u.id AND u.enabled=1 OR p.published IS NULL);
266259
```
267260

268261
### Unions

src/Database/SelectQuery.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -218,22 +218,6 @@ public function leftJoin(string $table, string $leftField, string $comparison, $
218218
return $this;
219219
}
220220

221-
/**
222-
* Cross Join.
223-
*
224-
* @param string $table Table name
225-
* @param string $leftField Name of the left field
226-
* @param string $comparison Comparison (=,<,>,<=,>=,<>,in, not in, between, not between)
227-
* @param mixed $rightField Value of the right field
228-
* @return self
229-
*/
230-
public function crossJoin(string $table, string $leftField, string $comparison, $rightField): self
231-
{
232-
$this->join[] = ['cross', $table, $leftField, $comparison, $rightField];
233-
234-
return $this;
235-
}
236-
237221
/**
238222
* Join with complex conditions.
239223
*
@@ -262,20 +246,6 @@ public function leftJoinRaw(string $table, RawExp $raw): self
262246
return $this;
263247
}
264248

265-
/**
266-
* Cross join with complex conditions.
267-
*
268-
* @param string $table Table name
269-
* @param RawExp $raw
270-
* @return self
271-
*/
272-
public function crossJoinRaw(string $table, RawExp $raw): self
273-
{
274-
$this->join[] = ['cross', $table, $raw, null, null, null];
275-
276-
return $this;
277-
}
278-
279249
/**
280250
* Where AND condition.
281251
*

tests/SelectQueryTest.php

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -622,25 +622,6 @@ public function testLeftJoinRaw()
622622
$this->assertSame("SELECT `id` FROM `test` LEFT JOIN `users` `u` ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c OR t2.b IS NULL);", $select->build());
623623
}
624624

625-
/**
626-
* Test
627-
*
628-
* @covers ::crossJoinRaw
629-
* @covers ::getJoinSql
630-
* @covers ::from
631-
* @covers ::prepare
632-
* @covers ::build
633-
*/
634-
public function testCrossJoinRaw()
635-
{
636-
$select = $this->select()
637-
->columns('id')
638-
->from('test')
639-
->crossJoinRaw('users u', new RawExp('t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c OR t2.b IS NULL'));
640-
$this->assertInstanceOf(PDOStatement::class, $select->prepare());
641-
$this->assertSame("SELECT `id` FROM `test` CROSS JOIN `users` `u` ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c OR t2.b IS NULL);", $select->build());
642-
}
643-
644625
/**
645626
* Test
646627
*
@@ -664,34 +645,6 @@ public function testLeftJoin()
664645
$this->assertSame($expected, $select->build());
665646
}
666647

667-
/**
668-
* Test
669-
*
670-
* @covers ::crossJoin
671-
* @covers ::getJoinSql
672-
* @covers ::leftJoin
673-
* @covers ::from
674-
* @covers ::prepare
675-
* @covers ::build
676-
*/
677-
public function testCrossJoin()
678-
{
679-
$select = $this->select()
680-
->columns('id')
681-
->from('test')
682-
->crossJoin('users u', 'u.id', '=', 'test.user_id');
683-
$this->assertInstanceOf(PDOStatement::class, $select->prepare());
684-
$this->assertSame("SELECT `id` FROM `test` CROSS JOIN `users` `u` ON `u`.`id` = `test`.`user_id`;", $select->build());
685-
686-
$select->crossJoin('table2 AS t2', 't2.id', '=', 'test.user_id');
687-
$expected = "SELECT `id` FROM `test` CROSS JOIN `users` `u` ON `u`.`id` = `test`.`user_id` CROSS JOIN `table2` AS `t2` ON `t2`.`id` = `test`.`user_id`;";
688-
$this->assertSame($expected, $select->build());
689-
690-
$select->leftJoin('table3 AS t3', 't3.id', '=', 'test.user_id');
691-
$expected = "SELECT `id` FROM `test` CROSS JOIN `users` `u` ON `u`.`id` = `test`.`user_id` CROSS JOIN `table2` AS `t2` ON `t2`.`id` = `test`.`user_id` LEFT JOIN `table3` AS `t3` ON `t3`.`id` = `test`.`user_id`;";
692-
$this->assertSame($expected, $select->build());
693-
}
694-
695648
/**
696649
* Test
697650
*

0 commit comments

Comments
 (0)