这个 MySQL 语句有什么作用?

发布于 2024-08-31 02:28:02 字数 497 浏览 5 评论 0原文

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`);

它来自开源项目,没有可用的文档。

特别是,cross-join 是什么意思?我只使用过 join/left join 。

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 .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

策马西风 2024-09-07 02:28:02

根据 MySQL 文档,它基本上是 < code>INNER JOIN,并且 INNER JOINJOIN 相同(即“INNER”是默认值) 。

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

烛影斜 2024-09-07 02:28:02

交叉连接: http://en.wikipedia.org/wiki/Join_% 28SQL%29#Cross_join

查询将选择结果插入到 PREFIX_tab_lang 中。选择的只是叉积中的两列。第三列——名称——实际上来自一个完全不同的选择,这也非常简单,只是其中一个 where 条件是另一个选择。

简而言之,这是我见过的最糟糕的查询之一。它的性能可能很糟糕,应该用一些受 TRANSATION 保护的代码或至少一个存储过程来替换它。

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.

七堇年 2024-09-07 02:28:02

您实际上可以将以下查询视为 MySQL 中的同义词:

SELECT      *
FROM        Table1
CROSS JOIN  Table2;

SELECT      *
FROM        Table1, Table2;

SELECT      *
FROM        Table1
INNER JOIN  Table2;

SELECT      *
FROM        Table1
JOIN        Table2;

测试用例:

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

所有四个查询都将返回以下结果集:

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文