MySQLは何の結果を示していない場合、クエリで空のピボットテーブル

hangerer:

MySQLの5.7.28のUbuntuでこのクエリ:

SELECT
    p.*
FROM
    testvvs as p,
    testvvs_privs AS priv
WHERE
    p.parent = 0 OR (p.userid = priv.userid AND priv.writepriv = 1)

ピボットテーブルPRIVが空の場合、正しい結果である場合、空の結果取得任意 PRIVの任意の値。どうしたの?

戦車や種類について

P.Salmon:

コンマが参加最初のテーブルにすべてが第二テーブルのすべてに接合されて、デカルト積を生成する - が、第2のテーブルが空の場合は何デカルト積は存在しません。第二のテーブルはそれで何かを持っている場合は、デカルト積があるでしょうし、あなた場合、条件を適用することができる場所

SELECT
    p.*,priv.*
FROM
    testvvs as p,
    testvvs_privs AS priv;

両方のシナリオについて、あなたは何が起こっているかが表示されます。

そう

MariaDB [sandbox]> drop table if exists testvvs,testvvs_privs;
Query OK, 0 rows affected (0.191 sec)

MariaDB [sandbox]> create table testvvs(userid int,parent int);
Query OK, 0 rows affected (0.233 sec)

MariaDB [sandbox]> insert into testvvs values(1,0);
Query OK, 1 row affected (0.031 sec)

MariaDB [sandbox]> create table testvvs_privs(userid int,writepriv int);
Query OK, 0 rows affected (0.184 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> SELECT
    ->     p.*,priv.*
    -> FROM
    ->     testvvs as p,
    ->     testvvs_privs AS priv;
Empty set (0.015 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> insert into testvvs_privs values(2,1);
Query OK, 1 row affected (0.015 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> SELECT
    ->     p.*,priv.*
    -> FROM
    ->     testvvs as p,
    ->     testvvs_privs AS priv;
+--------+--------+--------+-----------+
| userid | parent | userid | writepriv |
+--------+--------+--------+-----------+
|      1 |      0 |      2 |         1 |
+--------+--------+--------+-----------+
1 row in set (0.001 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> SELECT
    ->     p.*,priv.*
    -> FROM
    ->     testvvs as p,
    ->     testvvs_privs AS priv
    -> WHERE
    ->     p.parent = 0 OR (p.userid = priv.userid AND priv.writepriv = 1);
+--------+--------+--------+-----------+
| userid | parent | userid | writepriv |
+--------+--------+--------+-----------+
|      1 |      0 |      2 |         1 |
+--------+--------+--------+-----------+
1 row in set (0.001 sec)

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=373642&siteId=1