首页 > 解决方案 > PHP MySQL仅显示表中具有多条记录的唯一值

问题描述

我很难过,我不知道如何使用多个不同的标识符在表格上获取结果,然后对结果进行分组

简而言之,这是我的表ProjectFieldValue

id | project_id | textValue      | dateValue  | fieldKey
================================================================
1  | 1000       | Closed         | NULL       | contract_status
================================================================
2  | 1000       | NULL           | 2019-05-01 | closing_date
================================================================
3  | 1001       | Open           | NULL       | contract_status
================================================================
4  | 1001       | NULL           | 2019-05-22 | closing_date
================================================================
5  | 1002       | Closed         | NULL       | contract_status
================================================================
6  | 1002       | NULL           | 2019-05-11 | closing_date
================================================================
7  | 1003       | Closed         | NULL       | contract_status
================================================================
8  | 1003       | NULL           | 2019-05-24 | closing_date
================================================================

我需要运行查询以获取所有记录...

  1. “contact_status”的fieldKey和“Open”的那个fieldKey的textValue

  2. “close_date”的fieldKey和2019-05-01 AND 2019-05-30之间的dateValue

这是我尝试过的,我只是得到一个空白的查询结果集

select pfv.* 
from ProjectFieldValue pfv
where (pfv.dateValue between '2019-05-01' AND '2019-05-30' AND pfv.fieldKey = 'closing_date') 
AND (pfv.textValue = 'Closed' AND pfv.fieldKey = 'contract_status')

谢谢

标签: phpmysqlsymfony

解决方案


我认为您需要将您的选择拆分为 2 并进行子查询....查看此文档:

https://www.w3resource.com/mysql/subqueries/index.php


推荐阅读