MySQL shows no result when empty pivottable in query

hangerer :

This query in 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)

gets an empty result if pivottable priv is empty, correct result when any arbitrary value in priv. What's going on?

Tanks and kind regards

P.Salmon :

A comma join produces a cartesian product, everything on the first table is joined to everything on the second table - but there is no cartesian product if the second table is empty. If the second table has anything in it then there will be a cartesian product and the where condition can be applied if you

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

For both scenarios then you will see what's happening.

so

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)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=372603&siteId=1