繁体   English   中英

比较数据PostgreSQL

[英]Compare data postgresql

我里面有一个数据库和一个表。 这是我的桌子:

Houses (
    id,
    doors,
    windows,
    bathrooms,
    wallColor,
    dateRent
)

我想比较此表的数据并获取是否相等:例如:

Select id, doors, windows, bathrroms, wallColor
from Houses
where dateRent='01-02-2013' equal(
    Select id, doors, windows, bathrroms, wallColor
    from Houses
    where dateRent='08-09-2014'
);

真是假,真是太好了。

这将返回重复的房屋,如果没有则没有行

select id, doors, windows, bathrroms, wallColor
from Houses
where dateRent in ('01-02-2013', '08-09-2014')
group by 1, 2, 3, 4, 5
having count(*) > 1

如果要始终返回具有true或false值的一行:

with q as (
    select id, doors, windows, bathrroms, wallColor
    from Houses
    where dateRent in ('01-02-2013', '08-09-2014')
    group by 1, 2, 3, 4, 5
    having count(*) > 1
)
select exists (select 1 from q) as equal;

根据服务器配置,您将需要使用ISO日期格式YYYY-MM-DD或其他日期格式。

顺便说一句,您确定要在比较中包含id吗? 看起来像一个主键。

我会加入

SELECT DISTINCT houses1.id, coalesce(houses2.id, 'No Match') as Matched
FROM Houses h1
LEFT JOIN Houses h2 on h1.id=h2.id 
    AND h1.doors=h2.doors 
    AND h1.windows=h2.windows 
    AND h1.bathrooms=h2.bathrooms
    AND h1.wallColor=h2.wallColor 
    AND h2.dateRent='08-09-2014'
WHERE h1.dateRent='01-02-2013'

这将为您提供第一个日期中每个房屋的房屋编号,并比较第二个日期中是否至少有一个房屋具有相同数量的门,窗,浴室和墙壁颜色

它不会返回true或false,而是返回匹配的ID或“ No Match”(如果没有)。

但是,如果每个日期最多可以有一栋房子,那么一个对或错对您无济于事。 您将始终需要id字段。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM