We have a mechanism that lets a user view available Accelerators (attachments) for a given AsapStructure row (Node). They can bind multiple Accelerators to that individual Node.
I have two tables that are a many to many relationship (Has and belongs to Many)
AsapStructure hasAndBelongsToMany Accelerators
These two tables are joined via accelerators_nodes.
The scaled down schema is as follows:
AsapStructure:
- id
- title
- progress_status
- created
- modified
Accelerators:
- id
- title
- progress_status
- created
- modified
accelerators_nodes (Join Table)
- id
- node_id
- accelerator_id
- progress_status
- created
- modified
What I am trying to do, is select all ‘Accelerators’ that have a progress_status of 5, and the associated Node (AsapStructure – IF it exists. If it is NULL, i want to return the accelerator anyways, it just means that there is no relationship YET)
Each table has a progress_status field, this is just information about that particular row. In the case of the AsapStructure/Accelerator tables, this just indicates whether or not that document is approved.
For the join table, progress_status indicates whether or not that relationship has been approved.
The SQL I have generated gets close, but it selects duplicate records, it is as follows:
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
Sample Output
+------+------+------------+---------------------------------+-----------------+-------------------------------------------------------------------+----------+----------+---------------+-----------------+---------------------+
| 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 |
+------+------+------------+---------------------------------+-----------------+-------------------------------------------------------------------+----------+----------+---------------+-----------------+---------------------+
As you can see, the SECOND id column contains the ID of the Accelerator. The first id column is the ID of the AsapStructure row. You can see that there are duplicate rows in the second id field, 1686 and 1792 respectively.
The first row, you can see that the first id field is NULL, this just means that there is not a relationship between an AsapStructure row and that Accelerator.
My Goal
I want to select ALL available Accelerators, regardless of whether or not it is attached to an AsapStructure row. If it IS attached to an AsapStructure row – I ONLY want to return it if it matches XX (the Node ID of the document they are viewing and want to add attachments [Accelerators] to), otherwise return NULL.
The reason behind this, is we are displaying a paginated list, and the items in that list will display the status of the relationship, IF it exists. If it does not exist, we just give them an option to take action on that item.
Desired Result
Here is the (desired) output of the SQL query, as you can see it is selecting unique records, and also note the id‘s of the first id field.
+------+------+------------+---------------------------------+-----------------+-------------------------------------------------------------------+----------+----------+---------------+-----------------+---------------------+
| 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 |
+------+------+------------+---------------------------------+-----------------+-------------------------------------------------------------------+----------+----------+---------------+-----------------+---------------------+
This way, when we iterate over the data, we can easily determine:
- If it is bound to a document
- IF it is bound to a document, then we can determine the status of that relationship.
If there is an easier way to do this, I would love to hear it!
Did you try grouping by accelerator id?