SQL:当一行不同表格相等时选择数据> 1次

时间:2018-01-14 17:55:54

标签: sql

我有三张桌子:(这是一个例子)

  • 动物(id,type,legs,dni)
  • ciudad(id,name,people,province,dni)
  • persona(dni,姓名,年龄,电话,电子邮件)

例如,数据如下:

动物

+---+------+---+-----------+
| 1 | gato | 4 | 48063044C |
| 2 | gato | 4 | 48063044C |
| 3 | gato | 4 | 48063055D |
| 4 | gato | 4 | 48063055D |
+---+------+---+-----------+

+---+-----------+-------+-----------+-----------+
| 1 | barcelona | 10000 | barcelona | 48063044C |
| 2 | madrid    | 10000 | madrid    | 48063055D |
+---+-----------+-------+-----------+-----------+

假面

+-----------+--------+----+-----------+------------------+
| 48063044C | lluis  | 21 | 934444444 | lluis@gmail.com  |
| 48063055D | albert | 30 | 935555555 | albert@gmail.com |
+-----------+--------+----+-----------+------------------+

我想在查询中选择一些超过1 动物的人的数据。 (表动物中的字段DNI是所有者的DNI)。

我有这个:

SELECT p.dni as 'DNI', p.nombre as 'Nombre', p.edad as 'Edad',
       p.telefono as 'Telefono', p.email as 'Email',
       (SELECT c.nombre FROM ciudad c WHERE c.dni = p.dni) as 'Nombre ciudad'
FROM persona p 
INNER JOIN animal a ON a.dni = p.dni
HAVING COUNT(a.dni = p.dni) > 1

我只收到这些数据:

+-----------+-------+----+-----------+-----------------+-----------+
| 48063044C | lluis | 21 | 934444444 | lluis@gmail.com | barcelona |
+-----------+-------+----+-----------+-----------------+-----------+

如果错误,请纠正我,但我认为这不正常。我应该得到对方的数据,因为他们也有两只动物。

我知道与DNI的关系并不好,但这只是为了举例。

2 个答案:

答案 0 :(得分:0)

使用COUNT(p.dni)代替COUNT(a.dni = p.dni)。您也可以将子查询转换为连接,特别是因为您已经加入了动物表。

试试这个:

SELECT p.dni AS 'DNI', p.nombre AS 'Nombre', p.edad AS 'Edad', p.telefono AS 'Telefono',
       p.email AS 'Email', c.nombre AS 'Nombre ciudad'
FROM persona p 
INNER JOIN animal a ON a.dni = p.dni
LEFT JOIN ciudad c ON c.dni = p.dni
GROUP BY p.dni
HAVING COUNT(a.dni) > 1

答案 1 :(得分:0)

根据您的需要,以下查询将起作用,并为您提供所需的结果。 以下是postgresql查询

WITH animal AS (
SELECT 1::int AS id, 'gato'::varchar AS type, 4::int AS legs, '48063044C'::varchar AS dni
UNION ALL
SELECT 2::int AS id, 'gato'::varchar AS type, 4::int AS legs, '48063044C'::varchar AS dni
UNION ALL
SELECT 3::int AS id, 'gato'::varchar AS type, 4::int AS legs, '48063055D'::varchar AS dni
UNION ALL
SELECT 4::int AS id, 'gato'::varchar AS type, 4::int AS legs, '48063055D'::varchar AS dni
),
ciudad AS (
SELECT 1::int AS id, 'barcelona'::varchar AS name, 10000::int AS people, 'barcelona'::varchar AS province, '48063044C'::varchar AS dni
UNION ALL
SELECT 2::int AS id, 'madrid'::varchar AS name, 10000::int AS people, 'madrid'::varchar AS province, '48063055D'::varchar AS dni
),
persona AS (
SELECT '48063044C'::varchar AS dni, 'lluis'::varchar AS name, 21::int AS age, '934444444'::varchar AS phone, 'lluis@gmail.com'::varchar AS email
UNION ALL
SELECT '48063055D'::varchar AS dni, 'albert'::varchar AS name, 30::int AS age, '935555555'::varchar AS phone, 'albert@gmail.com'::varchar AS email
)
SELECT 
      p.dni AS DNI,
      p.name AS Nombre,
      p.age AS Edad,
      p.phone AS Telefono,
      p.email AS Email,
      c.name AS Nombre_ciudad
FROM 
    persona p 
    INNER JOIN animal a ON (a.dni = p.dni)
    LEFT JOIN ciudad c ON (c.dni = p.dni)
GROUP BY 
    p.dni,p.name,p.age,p.phone,p.email,c.name
HAVING 
    COUNT(a.dni) > 1