【问题标题】:searching the table by family using sql使用sql按家庭搜索表
【发布时间】:2019-05-04 00:28:00
【问题描述】:

我有这张表名为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 号 4 和 5 不应该存在。

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

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;

仍然有家庭的结果。

【问题讨论】:

    标签: php mysql sql date duplicates


    【解决方案1】:

    您的查询的问题是用于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 上测试

    【讨论】:

    • 非常感谢您的回复,它确实帮助了我:) :)
    【解决方案2】:

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

    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
    

    【讨论】:

    • 非常感谢您的回复我选择使用第一个答案,因为它更简单,:)
    猜你喜欢
    • 2019-04-29
    • 2021-09-24
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    相关资源
    最近更新 更多