views:

37

answers:

3
INSERT IGNORE INTO `PREFIX_tab_lang` (`id_tab`, `id_lang`, `name`)
    (SELECT `id_tab`, id_lang, (SELECT tl.`name`
        FROM `PREFIX_tab_lang` tl
        WHERE tl.`id_lang` = (SELECT c.`value`
            FROM `PREFIX_configuration` c
            WHERE c.`name` = 'PS_LANG_DEFAULT' LIMIT 1) AND tl.`id_tab`=`PREFIX_tab`.`id_tab`)
    FROM `PREFIX_lang` CROSS JOIN `PREFIX_tab`);

It's from an opensource project,and no documentation available.

Especially,what does cross-join mean? I've only used join/left join .

+2  A: 

According to the MySQL documentation, it's basically a synonym for INNER JOIN, and INNER JOIN is the same as just JOIN (that is, "INNER" is the default).

Dean Harding
So it's yet another a synonym for `JOIN` since `INNER JION` is the same as `JOIN`?
Yeah, I just added that to my answer. In "standard" SQL, `CROSS JOIN` is actually different to `INNER JOIN`, since `INNER JOIN` usually requires the predicate (`ON`) whereas `CROSS JOIN` does not.
Dean Harding
How about `CROSS JOIN` in "standard" SQL?
@user198729: http://en.wikipedia.org/wiki/Join_(SQL)#Cross_join
Daniel Vassallo
@Daniel Vassallo, so it's a synonym for `cartesian join`?
@user198729: Yes it is. It is MySQL that used the similar syntax for `INNER JOIN`s and `CROSS JOIN`s. Technically INNER JOINs should require the `ON` predicate as codeka noted in a comment above.
Daniel Vassallo
+1  A: 

Cross-join: http://en.wikipedia.org/wiki/Join_%28SQL%29#Cross_join

The query inserts into PREFIX_tab_lang the results of a select. The select is just two columns from the cross-product. The third column -- name -- actually comes from a totally different select, which is also pretty straight-forward except that one of it's where conditions is yet another select.

In short, this is one of the worst queries I've ever seen. It's preformance is probably horrible, and it should be replaced by a bit of TRANSATION-protected code or, at the very least, a stored procedure.

Igor
+1  A: 

You can actually consider the following queries to be synonyms in MySQL:

SELECT      *
FROM        Table1
CROSS JOIN  Table2;

SELECT      *
FROM        Table1, Table2;

SELECT      *
FROM        Table1
INNER JOIN  Table2;

SELECT      *
FROM        Table1
JOIN        Table2;

Test Case:

CREATE TABLE Table1 (id int, value varchar(10));
CREATE TABLE Table2 (id int, t1_id int);

INSERT INTO Table1 VALUES (1, 'Value 1');
INSERT INTO Table1 VALUES (2, 'Value 2');
INSERT INTO Table1 VALUES (3, 'Value 3');
INSERT INTO Table1 VALUES (4, 'Value 4');

INSERT INTO Table2 VALUES (1, 1);
INSERT INTO Table2 VALUES (2, 1);
INSERT INTO Table2 VALUES (3, 2);
INSERT INTO Table2 VALUES (4, 2);
INSERT INTO Table2 VALUES (5, 2);
INSERT INTO Table2 VALUES (6, 3);
INSERT INTO Table2 VALUES (7, 4);
INSERT INTO Table2 VALUES (8, 4);
INSERT INTO Table2 VALUES (9, 4);

All four queries would return the following result set:

+------+---------+------+-------+
| id   | value   | id   | t1_id |
+------+---------+------+-------+
|    1 | Value 1 |    1 |     1 |
|    2 | Value 2 |    1 |     1 |
|    3 | Value 3 |    1 |     1 |
|    4 | Value 4 |    1 |     1 |
|    1 | Value 1 |    2 |     1 |
|    2 | Value 2 |    2 |     1 |
|    3 | Value 3 |    2 |     1 |
|    4 | Value 4 |    2 |     1 |
|    1 | Value 1 |    3 |     2 |
|    2 | Value 2 |    3 |     2 |
|    3 | Value 3 |    3 |     2 |
|    4 | Value 4 |    3 |     2 |
|    1 | Value 1 |    4 |     2 |
|    2 | Value 2 |    4 |     2 |
|    3 | Value 3 |    4 |     2 |
|    4 | Value 4 |    4 |     2 |
|    1 | Value 1 |    5 |     2 |
|    2 | Value 2 |    5 |     2 |
|    3 | Value 3 |    5 |     2 |
|    4 | Value 4 |    5 |     2 |
|    1 | Value 1 |    6 |     3 |
|    2 | Value 2 |    6 |     3 |
|    3 | Value 3 |    6 |     3 |
|    4 | Value 4 |    6 |     3 |
|    1 | Value 1 |    7 |     4 |
|    2 | Value 2 |    7 |     4 |
|    3 | Value 3 |    7 |     4 |
|    4 | Value 4 |    7 |     4 |
|    1 | Value 1 |    8 |     4 |
|    2 | Value 2 |    8 |     4 |
|    3 | Value 3 |    8 |     4 |
|    4 | Value 4 |    8 |     4 |
|    1 | Value 1 |    9 |     4 |
|    2 | Value 2 |    9 |     4 |
|    3 | Value 3 |    9 |     4 |
|    4 | Value 4 |    9 |     4 |
+------+---------+------+-------+
36 rows in set (0.01 sec)
Daniel Vassallo