繁体   English   中英

使用SQL按家庭搜索表

[英]searching the table by family using sql

我有这个名为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;

而且依家还有结果。

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

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

您的查询的问题在于用于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

暂无
暂无

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

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