postgresql返回null但mysql没有

时间:2017-11-21 13:54:56

标签: mysql postgresql

我有一个应用程序,我将从Mysql迁移到Psql。 我有三个表t1,t2,t3如下所述。只要用户可用,表t3将始终具有条目,但如果用户未在其帐户中创建数据库,则表t1和t2可能也可能没有条目。

在mysql中执行q1时,我得到的结果集包含从表t3获取的值,即使t1和t2没有条目,但它在psql中返回null。所以我写了两个查询pq1和pq2等同于q1。 mysql没有返回空值但是psql的原因是什么?除了分解为psql的两个查询之外,还有更好的解决方案吗?

mysql查询(q1)

select 
COALESCE(sum(dr.NO_OF_QT),0), 
ur.NO_OF_USERS, ur.NO_OF_DB, 0, 
COALESCE(sum(dr.NO_OF_SM),0),
COALESCE(sum(dr.NO_OF_RPTS),0)  
from DataBaseProps dr
left join DataBaseDetails db on dr.DB_ID=db.ID and db.STATUS=1
left join UserProps ur on db.OWNER_UID=ur.USER_UID
where ur.USER_UID='USER_UID'

Psql-query_1(pq1)

select 
COALESCE(sum(dr.NO_OF_QT),0), 
0,0, 0, 
COALESCE(sum(dr.NO_OF_SM),0),
COALESCE(sum(dr.NO_OF_RPTS),0)  
from DataBaseProps dr
left join DataBaseDetails db on dr.DB_ID=db.ID and db.STATUS=1
left join UserProps ur on db.OWNER_UID=ur.USER_UID
where ur.USER_UID='USER_UID'

的psql-query_2(PQ2)

select NO_OF_USERS,NO_OF_DB from UserProps
where USER_UID='USER_UID'

表1 DataBaseProps(t1)

desc DataBaseProps >
+-----------------+------------+------+-----+---------+-------+
| Field           | Type       | Null | Key | Default | Extra |
+-----------------+------------+------+-----+---------+-------+
| DB_ID           | bigint(19) | NO   | PRI | NULL    |       |
| NO_OF_RPTS      | int(10)    | YES  |     | 0       |       |
| NO_OF_QT        | int(10)    | YES  |     | 0       |       |
| NO_OF_SM        | int(10)    | YES  |     | 0       |       |
+-----------------+------------+------+-----+---------+-------+

表2 - DataBaseDetails(t2)

 desc DataBaseDetails>
+-------------------+--------------+------+-----+-----------------+-------+
| Field             | Type         | Null | Key | Default         | Extra |
+-------------------+--------------+------+-----+-----------------+-------+
| ID                | bigint(19)   | NO   | PRI | NULL            |       |
| NAME              | varchar(50)  | NO   |     | NULL            |       |
| STATUS            | int(10)      | NO   |     | 1               |       |
| OWNER_UID         | bigint(19)   | NO   |     | NULL            |       |
+-------------------+--------------+------+-----+-----------------+-------+

表3 UserProps(t3)

desc UserProps>
+-----------------+------------+------+-----+---------+-------+
| Field           | Type       | Null | Key | Default | Extra |
+-----------------+------------+------+-----+---------+-------+
| USER_UID        | bigint(19) | NO   | PRI | NULL    |       |
| NO_OF_DB        | int(10)    | YES  |     | 0       |       |
| NO_OF_USERS     | int(10)    | YES  |     | 0       |       |
+-----------------+------------+------+-----+---------+-------+

0 个答案:

没有答案