【发布时间】:2012-04-04 17:42:55
【问题描述】:
我们有一种机制可以让用户查看给定 AsapStructure 行(节点)的可用加速器(附件)。他们可以将多个加速器绑定到那个单独的节点。
我有两个多对多关系的表(拥有和属于多)
AsapStructurehasAndBelongsToMany Accelerators
这两个表通过accelerators_nodes连接。
按比例缩小的架构如下:
AsapStructure:
- 身份证
- 标题
- 进度状态
- 已创建
- 修改
加速器:
- 身份证
- 标题
- 进度状态
- 已创建
- 修改
accelerators_nodes(加入表)
- 身份证
- node_id
- accelerator_id
- 进度状态
- 已创建
- 修改
我想要做的是选择所有进度状态为 5 的“加速器”以及相关的节点(AsapStructure - 如果它存在。如果它为 NULL,我无论如何都想返回加速器,这只是意味着还没有关系)
每个表都有一个 progress_status 字段,这只是有关该特定行的信息。对于 AsapStructure/Accelerator 表,这仅表明该文档是否被批准。
对于连接表,progress_status 表示该关系是否已被批准。
我生成的SQL接近了,但是它选择了重复的记录,如下:
SELECT
AsapStructure.id,
AsapAccelerator.id,
AsapAccelerator.foreign_id,
AsapAccelerator.title,
AcceleratorsNode.progress_status,
AsapAccelerator.filename,
AsapAccelerator.filesize,
AsapAccelerator.language,
AsapAccelerator.doctype,
AsapAccelerator.progress_status,
AsapAccelerator.modified
FROM
accelerators AS AsapAccelerator
left JOIN
accelerators_nodes AS AcceleratorsNode ON (AcceleratorsNode.accelerator_id = AsapAccelerator.id)
left JOIN
asap_structure AS AsapStructure ON (AcceleratorsNode.node_id = AsapStructure.id)
WHERE
AsapAccelerator.progress_status = 5
ORDER BY AsapStructure.id ASC, AsapAccelerator.title ASC
LIMIT 50
样本输出
+------+------+------------+---------------------------------+-----------------+-------------------------------------------------------------------+----------+----------+---------------+-----------------+---------------------+
| id | id | foreign_id | title | progress_status | filename | filesize | language | doctype | progress_status | modified |
+------+------+------------+---------------------------------+-----------------+-------------------------------------------------------------------+----------+----------+---------------+-----------------+---------------------+
| NULL | 1963 | 1211 | Value Delivery Scorecard | NULL | 484_Value_Delivery_Status_Project_Scorecard_Template_MS_Draft.doc | 113152 | English | Template Form | 5 | 2012-03-05 23:56:53 |
| 1 | 1686 | 933 | Going Live Check Sample Report | 4 | 459_SAP_GoingLive_Check_Analysis_Session_Sample_Report.doc | 779264 | English | Sample | 5 | 2012-03-05 23:56:21 |
| 1 | 1792 | 1100 | Preliminary Cutover Strategy | 4 | 471_Preliminary_Cutover_Strategy.ppt | 1070080 | English | Presentation | 5 | 2012-03-05 23:56:33 |
| 933 | 1686 | 933 | Going Live Check Sample Report | 5 | 459_SAP_GoingLive_Check_Analysis_Session_Sample_Report.doc | 779264 | English | Sample | 5 | 2012-03-05 23:56:21 |
| 1100 | 1792 | 1100 | Preliminary Cutover Strategy | 5 | 471_Preliminary_Cutover_Strategy.ppt | 1070080 | English | Presentation | 5 | 2012-03-05 23:56:33 |
| 1151 | 1894 | 1151 | Cutover Communications | 5 | 56_Cutover_Communications.xls | 120320 | English | Template Form | 5 | 2012-03-05 23:56:45 |
+------+------+------------+---------------------------------+-----------------+-------------------------------------------------------------------+----------+----------+---------------+-----------------+---------------------+
如您所见,第二个id 列包含加速器的 ID。第一个 id 列是 AsapStructure 行的 ID。可以看到在第二个id 字段中有重复的行,分别是1686 和1792。
第一行,你可以看到第一个id字段是NULL,这只是意味着AsapStructure行和那个加速器之间没有关系。
我的目标
我想选择所有可用的加速器,无论它是否附加到 AsapStructure 行。如果它附加到 AsapStructure 行 - 如果它与 XX 匹配(他们正在查看的文档的节点 ID 并希望将附件 [Accelerators] 添加到),我只想返回它,否则返回 NULL。
这背后的原因是,我们正在显示一个分页列表,并且该列表中的项目将显示关系的状态,如果它存在的话。如果它不存在,我们只是给他们一个选项来对该项目采取行动。
期望的结果
这是 SQL 查询的(期望的)输出,您可以看到它正在选择唯一记录,并注意第一个 id 字段的 id。
+------+------+------------+---------------------------------+-----------------+-------------------------------------------------------------------+----------+----------+---------------+-----------------+---------------------+
| id | id | foreign_id | title | progress_status | filename | filesize | language | doctype | progress_status | modified |
+------+------+------------+---------------------------------+-----------------+-------------------------------------------------------------------+----------+----------+---------------+-----------------+---------------------+
| NULL | 1963 | 1211 | Value Delivery Scorecard | NULL | 484_Value_Delivery_Status_Project_Scorecard_Template_MS_Draft.doc | 113152 | English | Template Form | 5 | 2012-03-05 23:56:53 |
| 1 | 1686 | 933 | Going Live Check Sample Report | 4 | 459_SAP_GoingLive_Check_Analysis_Session_Sample_Report.doc | 779264 | English | Sample | 5 | 2012-03-05 23:56:21 |
| 1 | 1792 | 1100 | Preliminary Cutover Strategy | 4 | 471_Preliminary_Cutover_Strategy.ppt | 1070080 | English | Presentation | 5 | 2012-03-05 23:56:33 |
| NULL | 1894 | 1151 | Cutover Communications | 5 | 56_Cutover_Communications.xls | 120320 | English | Template Form | 5 | 2012-03-05 23:56:45 |
+------+------+------------+---------------------------------+-----------------+-------------------------------------------------------------------+----------+----------+---------------+-----------------+---------------------+
这样,当我们对数据进行迭代时,我们可以很容易地确定:
- 如果绑定到文档
- 如果它绑定到一个文档,那么我们可以确定该关系的状态。
如果有更简单的方法可以做到这一点,我很想听听!
【问题讨论】:
标签: mysql cakephp join cakephp-1.3 has-and-belongs-to-many