【发布时间】: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