1

所以我有两个问题,第一个问题根据 $_SESSION 选择一个数组,然后在第二个选择中使用。

我的问题是,例如,当我使用一个条目时,每件事都可以,表格将如下所示:

|DATA| USER1|
|DATA| USER2|
|DATA| USER3|

但是,如果我必须为同一个用户输入条目,我会得到这样的重复:

|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]

我的代码如下所示:

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

     }
}

所以我不知道为什么当我有两个条目时它会重复,请帮忙?

PS:如果我为同一用户输入 3 个条目,则同一用户将输入 6 个条目,也会重复。

4

1 回答 1

3

尝试添加DISTINCT到您的第一个查询:

SELECT DISTINCT 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];

另外,我会避免使用隐式连接,尤其是在将它们与显式外连接混合时。它使查询更难阅读,并且(IMO)更容易出错。请改用INNER JOIN语法。

您的第二个查询看起来更像这样:

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
    INNER JOIN task_assignment T
        ON T.task_type_id = TT.task_type_id
    INNER JOIN orders O
        ON O.order_id = T.order_id
    INNER JOIN users UO
        ON UO.user_id = O.user_id
    LEFT JOIN users U
        ON T.assigned_to = U.user_id
WHERE
    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
于 2012-09-20T12:56:12.880 回答