我把这张table命名为 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
我想要这张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 |
| 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 |
+-----+--------------------+----------+--------------+------+
你看到身份证号码了吗 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;
还有家人的调查结果。
2条答案
按热度按时间6tdlim6h1#
选择的问题是,您还需要使用相同的用户名筛选子查询,例如:
xggvc2p62#
查询的问题是用于
IN
还从其他用户中选择“族”。你可以通过包含用户来解决这个问题。
但在这种情况下,使用
EXISTS
相反。一个关于用户和家庭的链接。
然后将这些额外的条件放入exists查询中。
在测试仪上测试