在 2 个表之一中查找记录

发布于 2024-12-06 06:40:10 字数 1041 浏览 1 评论 0原文

在 PostgreSQL 8.4.8 数据库中,我有 2 个表:noresettrack

它们具有完全相同的列名,并包含由唯一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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏了南城 2024-12-13 06:40:10

你应该使用union:(

select qdatetime, id, beta_prog, emailid, formfactor, 'noreset' as tableOrigin
    from noreset where id='20110922124020305'
union
select qdatetime, id, beta_prog, emailid, formfactor, 'track' as tableOrigin
    from track where id='20110922124020305'

union删除重复的行,如果你想全部使用union all

如果你经常使用这个是更好地进行视图:

CREATE VIEW yourviewname as 
select qdatetime, id, beta_prog, emailid, formfactor, 'noreset' as tableOrigin
    from noreset
union
select qdatetime, id, beta_prog, emailid, formfactor, 'track' as tableOrigin
    from track

然后您的查询将是:

SELECT qdatetime, id, beta_prog, emailid, formfactor
  FROM yourviewname
 WHERE id='20110922124020305'

You should use union:

select qdatetime, id, beta_prog, emailid, formfactor, 'noreset' as tableOrigin
    from noreset where id='20110922124020305'
union
select qdatetime, id, beta_prog, emailid, formfactor, 'track' as tableOrigin
    from track where id='20110922124020305'

(union remove duplicate lines, if you want all use union all)

If you are goint to use this very often is better to make a view:

CREATE VIEW yourviewname as 
select qdatetime, id, beta_prog, emailid, formfactor, 'noreset' as tableOrigin
    from noreset
union
select qdatetime, id, beta_prog, emailid, formfactor, 'track' as tableOrigin
    from track

and then your query would be:

SELECT qdatetime, id, beta_prog, emailid, formfactor
  FROM yourviewname
 WHERE id='20110922124020305'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文