I am trying to make a query with mySql but the result is not as expected as rows are repeated and some data have no relation at all.
I need to select from the table list_task
the NAME
and the PUBLIC_NUMBER
and the table list_project
the NAME
and the PUBLIC_NUMBER
that refers to a user X.
So, each project ( list_project
) has zero or many columns ( list_column
) and each column has zero or many tasks ( list_task
). A user can create projects ( ADMIN_USER
), columns ( USER_ID
) and tasks ( USER_ID
). The list_myprojects
table stores all the projects that a user has access to, while the list_projects
table stores only the project information.
The query you created was this:
SELECT list_task.PUBLIC_NUMBER AS TASK_NUMBER, list_task.NAME AS TASK_NAME, list_project.NAME AS PROJECT_NAME, list_project.PUBLIC_NUMBER AS PROJECT_NUMBER
FROM list_task
JOIN list_myprojects ON list_myprojects.USER_ID = list_task.USER_ID
JOIN list_project ON list_myprojects.PUBLIC_NUMBER = list_project.PUBLIC_NUMBER
WHERE list_task.COLUMN_NUMBER IN
(SELECT list_column.PUBLIC_NUMBER
FROM list_column
JOIN list_myprojects ON list_column.PROJECT_ID = list_myprojects.PUBLIC_NUMBER
WHERE list_myprojects.USER_ID = 'R8D0F1Z6h6W5K9g8n4U5' )
AND list_myprojects.USER_ID = 'R8D0F1Z6h6W5K9g8n4U5'
As you can see in the following image the results are duplicated and wrong because the same TASK_NUMBER
belongs to two PROJECT_NUMBER
different.
I hope you have explained me well