使用SQL按家庭搜索表

时间:2018-12-02 14:57:44

标签: php mysql sql date duplicates

我有一个名为table的表:

+-----+--------------------+----------+--------------+------+
| id  |       money        |  family  |     date     | user |
+-----+--------------------+----------+--------------+------+
|  1  |  credit card       |       1  | 2018-01-04   | U123 |
|  2  |  direct transfert  |       1  | 2018-01-04   | U123 |
|  3  |  Wire transfert    |       1  | 2018-01-06   | U123 |
|  4  |  Exchange          |       2  | 2018-01-03   | U123 |
|  5  |  free              |       2  | 2018-01-03   | U123 |
|  6  |  other             |       3  | 2018-01-08   | U123 |
+-----+--------------------+----------+--------------+------+

我想要的是始终由家人获得结果。例如,如果我搜索date >= 2018-01-06,我想获得该表:

+-----+--------------------+----------+--------------+------+
| id  |       money        |  family  |     date     | user |
+-----+--------------------+----------+--------------+------+
|  1  |  credit card       |       1  | 2018-01-04   | U123 |
|  2  |  direct transfert  |       1  | 2018-01-04   | U123 |
|  3  |  Wire transfert    |       1  | 2018-01-06   | U123 |
|  6  |  other             |       3  | 2018-01-08   | U123 |
+-----+--------------------+----------+--------------+------+

我尝试了以下查询:

SELECT e.* 
  FROM table e 
 WHERE e.user ='U123' AND e.family IN 
(
    SELECT e2.family 
      FROM table e2 
     WHERE (e2.date >= "2018-01-06") 
) 
 ORDER BY family;

但是我收到以下结果:

+-----+--------------------+----------+--------------+------+
| id  |       money        |  family  |     date     | user |
+-----+--------------------+----------+--------------+------+
|  1  |  credit card       |       1  | 2018-01-04   | U123 |
|  2  |  direct transfert  |       1  | 2018-01-04   | U123 |
|  3  |  Wire transfert    |       1  | 2018-01-06   | U123 |
|  4  |  Exchange          |       2  | 2018-01-03   | U123 |
|  5  |  free              |       2  | 2018-01-03   | U123 |
|  6  |  other             |       3  | 2018-01-08   | U123 |
+-----+--------------------+----------+--------------+------+

您看到ID号45不应该在那儿。

此外,我希望能够添加其他类似条件:

SELECT e.*
  FROM table e 
 WHERE e.user ='U123' AND e.family IN 
(
    SELECT e2.family 
      FROM table e2 
     WHERE (e2.date >= "2018-01-06") 
       AND (e2.money like "%transf%")
)
 ORDER BY family;

仍然按家庭列出结果。

2 个答案:

答案 0 :(得分:0)

选择的问题在于,您还需要使用相同的用户名来过滤子查询,例如:

select e.* 
from table e 
where e.user ='u123' and e.family in 
(
    select distinct e2.family 
    from table e2 
    where 
        e2.user ='u123' and 
        e2.date >= '2018-01-06'
) 
order by e.family
select e.*
from table e 
where e.user ='u123' and e.family in 
(
    select distinct e2.family 
    from table e2 
    where 
        e2.user ='u123' and 
        e2.date >= '2018-01-06' and
        e2.money like '%transf%'
)
order by e.family

答案 1 :(得分:0)

查询的问题是用于IN的子查询还会选择其他用户的“家庭”。

您可以通过包含用户来解决此问题。

SELECT *
FROM yourtable e 
WHERE e.user = 'U123'
  AND (e.user, e.family) IN (
    SELECT DISTINCT e2.user, e2.family 
    FROM yourtable e2 
    WHERE e2.date >= '2018-01-06'
  )
ORDER BY family;

但是在这种情况下,最好使用EXISTS
与用户和家人联系在一起的一种。

然后将这些额外条件放入EXISTS查询中。

SELECT *
FROM yourtable e 
WHERE e.user = 'U123'
  AND EXISTS (
    SELECT 1
    FROM yourtable e2
    WHERE e2.user = e.user
      AND e2.family = e.family
      AND e2.date >= '2018-01-06'
      -- AND e2.money LIKE '%transf%'
  )
ORDER BY family;

在RexTester here上测试