Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL在查询中为空数据透视表时不显示结果_Mysql - Fatal编程技术网

MySQL在查询中为空数据透视表时不显示结果

MySQL在查询中为空数据透视表时不显示结果,mysql,Mysql,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中有任意值,则更正结果。发生了什么 坦克和善良的问候逗号连接产生笛卡尔乘积,第一个表上的所有内容都连接到第二个表上的

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中有任意值,则更正结果。发生了什么


坦克和善良的问候

逗号连接产生笛卡尔乘积,第一个表上的所有内容都连接到第二个表上的所有内容-但如果第二个表为空,则没有笛卡尔乘积。如果第二个表中有任何内容,那么将有一个笛卡尔积,如果

SELECT
    p.*,priv.*
FROM
    testvvs as p,
    testvvs_privs AS priv;
对于这两种情况,您将看到发生了什么

所以


你真的应该使用JOIN,可能是LEFT-JOIN。谢谢你的提示-我会试试的。但是为什么它不按我的方式工作呢?是的,似乎是这样-表数据不太集中,p-table中有一些条目匹配条件parent=0以及priv table中的userid和writepriv字段,否则就没有数据:没有给出结果。如果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)