Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inner join on true

My table:

   abc
+--------+
| letter |
+--------+
| a      |
| b      |
+--------+

Query:

SELECT *
FROM abc t1 INNER JOIN abc t2
ON true

Result:

+--------+--------+
| letter | letter |
+--------+--------+
| a      | a      |
| b      | a      |
| a      | b      |
| b      | b      |
+--------+--------+

What does or how does ON true do/work?

like image 862
Robert Rocha Avatar asked Jan 28 '26 12:01

Robert Rocha


1 Answers

While I've never seen this exact syntax (for obvious reasons - it really serves little purpose and I wouldn't expect to encounter it in the real world) my guess would be that the result will be a cartesian product as the condition states that rows in t1 should be matched with rows in t2 when condition evaluates as true, which it always does. So all rows in the first set will be matched to all rows in the second set.

The condition is never tried against anything else, just itself and true will always evaluate as true.

like image 88
jpw Avatar answered Jan 30 '26 02:01

jpw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!