So I have a two quesies that the first one select an array based on the $_SESSION and then is used in the second select.
My problem is that when I have for example for one use one entrie every thing is ok the table will be like this:
|DATA| USER1|
|DATA| USER2|
|DATA| USER3|
But if I have to entries for the same user I got duplicate like this:
|DATA| USER1| [1]
|DATA| USER1| [2]
|DATA| USER2|
|DATA| USER3|
|DATA| USER1| DUPLICATE of FIRST ENTRY USER ABOVE [1]
|DATA| USER1| DUPLICATE OF SECOND ENTRY USER ABOVE [2]
My code looks like this :
function make_table($user_id){
first_query with this select `Select U.user_id
from users U, tasks T
where T.assigned_to = U.user_id
and T.status='Assigned'
and U.parent_id = $_SESSION[userid]`;
foreach ($query_result_worker as $row){
$user_id_worker = $row['user_id'];
//second_query with this select
SELECT T.task_id, T.job_server_id, T.TIMESTAMP, TT.task_type_name, T.STATUS,
UO.username AS customer_name, UO.user_id AS customer_id, T.STATUS, T.quantity,
T.order_id, U.username, T.priority, T.assigned_time, U.username, O.order_id
FROM task_type TT, orders O, users UO, task_assignment T
LEFT JOIN users U
ON T.assigned_to = U.user_id
WHERE T.task_type_id = TT.task_type_id
AND O.order_id = T.order_id
AND O.user_id = UO.user_id
AND T.STATUS = 'Assigned'
AND T.assigned_to = $ROW [user_id]
AND T.TIMESTAMP <= CURRENT_TIMESTAMP
ORDER BY T.priority DESC, DATE (T.TIMESTAMP), T.quantity DESC, T.task_id
foreach ($result as $tablerow) {
make the printing table stuff
}
}
So I don’t know why it’s duplicating when I got two entries, any help please?
PS: if I have 3 entries for the same user it will be 6 for the same user, duplicates too.
Try adding
DISTINCTto your first query:Also, I would avoid using implicit joins, especially when mixing them with explicit outer joins. It makes the query a bit harder to read, and (IMO) easier to make a mistake. Use the
INNER JOINsyntax instead.Your second query would look more like this: