在 2 个表之一中查找记录
在 PostgreSQL 8.4.8 数据库中,我有 2 个表:noreset 和 track。
它们具有完全相同的列名,并包含由唯一id标识的记录。
一条记录只能出现在其中一个表中,例如:
# select qdatetime, id, beta_prog, emailid, formfactor
from noreset where id='20110922124020305';
qdatetime | id | beta_prog | emailid | formfactor
---------------------+-------------------+-----------+------------------+------------
2011-09-22 11:39:24 | 20110922124020305 | unknown | 4bfa32689adf8189 | 21
(1 row)
# select qdatetime, id, beta_prog, emailid, formfactor
from track where id='20110922124020305';
qdatetime | id | beta_prog | emailid | formfactor
-----------+----+-----------+---------+------------
(0 rows)
我正在尝试提出一个连接语句,它将在两个表之一中按 id 查找一条记录。背景是我有一个 PHP 脚本,它总是使用 1 个表,但现在突然要求我在两个表中搜索。
这可以做到吗?它是完全外连接吗?我很困惑如何在 SQL 查询中指定列名(即我必须在前面添加一个表 id,但是是哪一个?)...
In PostgreSQL 8.4.8 database I have 2 tables: noreset and track.
They have exactly same column names and contain records identified by unique id.
A record can be present only in one of the tables, for example:
# select qdatetime, id, beta_prog, emailid, formfactor
from noreset where id='20110922124020305';
qdatetime | id | beta_prog | emailid | formfactor
---------------------+-------------------+-----------+------------------+------------
2011-09-22 11:39:24 | 20110922124020305 | unknown | 4bfa32689adf8189 | 21
(1 row)
# select qdatetime, id, beta_prog, emailid, formfactor
from track where id='20110922124020305';
qdatetime | id | beta_prog | emailid | formfactor
-----------+----+-----------+---------+------------
(0 rows)
I'm trying to come up with a join statement, which would find a record by id in one of the both tables. The background is that I have a PHP-script, which was always using 1 table, but now suddenly I'm requested to search in both tables.
Can this be done? Is it a full outer join? I'm confused how to specify the column names in my SQL query (i.e. I must prepend a table id, but which one?)...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你应该使用
union
:(union
删除重复的行,如果你想全部使用union all
)如果你经常使用这个是更好地进行视图:
然后您的查询将是:
You should use
union
:(
union
remove duplicate lines, if you want all useunion all
)If you are goint to use this very often is better to make a view:
and then your query would be: