开发者

What does this MySQL statement do?

开发者 https://www.devze.com 2022-12-28 18:03 出处:网络
INSERT IGNORE INTO `PREFIX_tab_lang` (`id_tab`, `id_lang`, `name`) (SE开发者_如何学JAVALECT `id_tab`, id_lang, (SELECT tl.`name`
INSERT IGNORE INTO `PREFIX_tab_lang` (`id_tab`, `id_lang`, `name`)
    (SE开发者_如何学JAVALECT `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 .


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).


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.


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号