[英]Mysql & PHP search table for records whose value is between two dates and other record values
I have a table similar to this: 我有一个与此类似的表:
-----------------------------------------------------------------------
id | project_id | dateValue | textValue | key
-----------------------------------------------------------------------
1 | 1 | 2018-10-25 | NULL | closing_date
-----------------------------------------------------------------------
2 | 2 | 2018-10-26 | NULL | listing_date
-----------------------------------------------------------------------
3 | 3 | 2018-10-27 | NULL | closing_date
-----------------------------------------------------------------------
4 | 1 | NULL | Pending | contract_status
-----------------------------------------------------------------------
5 | 2 | NULL | Active | contract_status
-----------------------------------------------------------------------
6 | 3 | NULL | Pending acc. | contract_status
-----------------------------------------------------------------------
I cannot get this to return any results.. Any suggestions? 我无法获得任何结果。.有什么建议吗? I need it to return project_ids "1 & 3" 我需要它来返回project_ids“ 1&3”
SELECT t.*
FROM Table t
WHERE ((t.dateValue BETWEEN '2018-10-24' AND '2018-10-27') AND (t.key = 'contract_status' AND t.textValue IN ('Pending','Pending acc.'))
GROUP BY t.project_id
I've tried this variation as well: 我也尝试过这种变化:
SELECT t.*
FROM Table t
WHERE ((t.dateValue BETWEEN '2018-10-24' AND '2018-10-27') AND (t.key IN ('contract_status', 'closing_date') AND t.textValue IN ('Pending','Pending acc.'))
GROUP BY t.project_id
Thanks! 谢谢!
Your DB schema is weird and goal is not clear. 您的数据库架构很奇怪,目标不明确。
Here is something you can use to understand why you are not getting any result: 您可以使用以下内容来了解为什么未获得任何结果:
http://sqlfiddle.com/#!9/701a65/3 http://sqlfiddle.com/#!9/701a65/3
SELECT t.project_id,
MAX(t.dateValue),
MAX(t.textValue)
FROM t
WHERE (t.dateValue BETWEEN '2018-10-24' AND '2018-10-27'
AND (t.key = 'closing_date'))
OR t.textValue IN ('Pending','Pending acc.')
GROUP BY t.project_id
But you should definitely redesign your DB and read more about relational databases. 但是,您绝对应该重新设计数据库,并阅读有关关系数据库的更多信息。
Your main problem is your WHERE
clause. 您的主要问题是您的WHERE
子句。
WHERE ((t.dateValue BETWEEN '2018-10-24' AND '2018-10-27')
AND (t.key = 'contract_status'
AND t.textValue IN ('Pending','Pending acc.'))
You use AND
everywhere that means you want to find all records which has for example dateValue
AND textValue
- but in your raw data there is no such records at all. 在任何地方都使用AND
,这意味着您想查找所有具有dateValue
和textValue
类的记录-但在原始数据中根本没有这样的记录。 :-) :-)
Since your dataset does not cover all the conditions in one single row. 由于您的数据集未在一行中包含所有条件。 You may use conditional aggregation with Having
clause: 您可以将条件聚合与Having
子句一起使用:
SELECT t.project_id,
SUM(IF(t.dateValue BETWEEN '2018-10-24'
AND '2018-10-27',
1, 0)) AS date_check,
SUM(IF(t.key = 'contract_status' AND
t.textValue IN ('Pending','Pending acc.'),
1, 0)) AS key_check
FROM Table t
GROUP BY t.project_id
HAVING date_check > 0 AND
key_check > 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.