Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.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
PostgreSQL:左外部联接语法_Postgresql_Left Join_Outer Join - Fatal编程技术网

PostgreSQL:左外部联接语法

PostgreSQL:左外部联接语法,postgresql,left-join,outer-join,Postgresql,Left Join,Outer Join,我在CentOS 5.5上使用PostgreSQL 8.4.6,并有一个用户表: # select * from pref_users where id='DE2'; id | first_name | last_name | female | avatar | city | lat | lng | login | last_ip | medals | logout -----+-

我在CentOS 5.5上使用PostgreSQL 8.4.6,并有一个用户表:

# select * from pref_users where id='DE2';
 id  | first_name | last_name | female |      avatar      |        city         | lat | lng |           login            | last_ip | medals |           logout
-----+------------+-----------+--------+------------------+---------------------+-----+-----+----------------------------+---------+--------+----------------------------
 DE2 | Alex       |           | f      | 2_1280837766.jpg | г. Бохум в Германии |     |     | 2011-01-02 19:26:37.790909 |         |        | 2011-01-02 19:29:30.197062
(1 row)
还有一张表格列出了他们每周在游戏中赢得的“虚拟货币”:

# select * from pref_money where id='DE2';
 id  | money |   yw
-----+-------+---------
 DE2 |    66 | 2010-48
(1 row)
然后,我尝试为用户显示这两个信息,但我只对用户本周的钱感兴趣:

# select u.id,
    u.first_name,
    u.city,
    u.avatar,
    m.money,
    u.login > u.logout as online
from pref_users u, pref_money m where
    m.yw=to_char(current_timestamp, 'YYYY-IW') and
    u.id=m.id and
    u.id='DE2'
order by m.money desc;
 id | first_name | city | avatar | money | online
----+------------+------+--------+-------+--------
(0 rows)
在本例中,我没有行,因为用户“DE2”本周还没有赚到任何“虚拟货币”

我想更改我的查询,使其始终返回现有用户的数据,如果他们本周没有玩过游戏,则查询应返回0

所以我想我需要一个

 select u.id,
    u.first_name,
    u.city,
    u.avatar,
    m.money,
    u.login > u.logout as online
from pref_users u left outer join pref_money m on (
    m.yw=to_char(current_timestamp, 'YYYY-IW') and
    u.id=m.id and
    u.id='DE2')
order by m.money desc;
但是这些返回给我很多不同用户的行,而不是id='DE2'的行


请问我做错了什么?

您想将筛选条件
u.id='DE2'
放在WHERE子句中,而将其他所有内容都放在ON子句中。对于ON不匹配的每个用户,它仍然会输出带有u数据的行和m的空值。

尝试移动

u.id='DE2'
插入WHERE子句,如下所示:

select u.id,
    u.first_name,
    u.city,
    u.avatar,
    m.money,
    u.login > u.logout as online
from pref_users u left outer join pref_money m on u.id=m.id
where m.yw=to_char(current_timestamp, 'YYYY-IW') and
    u.id='DE2'
order by m.money desc;

编辑:将m.yw添加回on子句
select u.id,
    u.first_name,
    u.city,
    u.avatar,
    m.money,
    u.login > u.logout as online
from pref_users u left outer join pref_money m on u.id=m.id
where m.yw=to_char(current_timestamp, 'YYYY-IW') and
    u.id='DE2'
order by m.money desc;