我有以下查詢;SELF-JOIN丟棄true CROSS JOIN行
我得到的是門票信息。我使用自聯接,以獲得請求者和同一行中的受讓人:
SELECT z.id AS TICKET, z.name AS Subject, reqs.name AS Requester, techs.name AS Assignee,
e.name AS Entity,DATE_FORMAT(tt.date,'%y%-%m%-%d') AS DATE,
DATE_FORMAT(tt.date,'%T') AS HOUR,
CASE WHEN z.priority = 6 THEN 'Mayor' WHEN z.priority = 5 THEN 'Muy urgente' WHEN z.priority = 4 THEN 'Urgente'WHEN z.priority = 3 THEN 'Mediana' WHEN z.priority = 2 THEN 'Baja' WHEN z.priority =1 THEN 'Muy baja' END AS Priority,
c.name AS Category, i.name AS Department
FROM glpi_tickets_users tureq
JOIN glpi_tickets_users tutech ON tureq.tickets_id = tutech.tickets_id
JOIN glpi_users AS reqs ON tureq.users_id = reqs.id
JOIN glpi_users AS techs ON tutech.users_id = techs.id
JOIN glpi_tickets z ON z.id = tureq.tickets_id
LEFT OUTER JOIN glpi_tickettasks tt ON z.id = tt.tickets_id
LEFT JOIN glpi_itilcategories i ON z.itilcategories_id = i.id
LEFT JOIN glpi_usercategories c ON c.id = reqs.usercategories_id
INNER JOIN glpi_entities e ON z.entities_id = e.id
WHERE (tureq.id < tutech.id AND tureq.type < tutech.type) OR
(tureq.id < tutech.id AND tureq.users_id = tutech.users_id) OR
(tureq.id = tutech.id AND tureq.users_id = tutech.users_id)
的問題是,我得到類似的東西:
1 Report jdoe jdoe Development 16-06-07 11:56:17 Mediana Software Mkt
1 Report jdoe fwilson Development 16-06-07 11:56:17 Mediana Software MKt
1 Report fwilson fwilson Development 16-06-07 11:56:17 Mediana Software Mkt
2 Task11 gwilliams gwilliams Ops 16-06-08 12:00:00 ALTA Hardware Def
3 Task12 gwilliams gwilliams Ops 16-06-08 12:01:00 ALTA Hardware Def
我不想第一和第三排做因爲是CROSS JOIN結果。第二行是好的,因爲jdoe是請求者,fwilson是受讓人。
問題是,有時候請求者和受讓人是相同的,例如:他爲自己將要執行的任務創建票據。例如,第4行和第5行都可以。
所以,我應該怎麼做才能讓那些不同的情況有差別,即:我需要包括:
tureq.id = tech.id AND req.users_id = tech.users.id
但如果已經存在
tureq.id = tech.id AND req.users_id <> tech.users_id
更新
主要問題是用戶可以給自己分配一張票:
SELECT * from glpi_tickets_users WHERE type = 2 GROUP BY tickets_id HAVING COUNT(users_id)<2 limit 3;
+----+------------+----------+------+------------------+-------------------+
| id | tickets_id | users_id | type | use_notification | alternative_email |
+----+------------+----------+------+------------------+-------------------+
| 1 | 2 | 12 | 2 | 1 | NULL |
| 3 | 6 | 13 | 2 | 1 | NULL |
| 7 | 8 | 14 | 2 | 1 | NULL |
+----+------------+----------+------+------------------+-------------------+
更新2:
這是一個人的錯。問題實際上不是關於自我分配的票證。相反,有些票據不是Requester或Requester,但仍未分配任何解析器。 我找到了
請閱讀有關如何創建一個MCVE([MCVE])。一個包含9個表的查詢不太可能是最小的;如果是的話,會有解釋你如何證明它很少。 –
我想你需要在加入glpi_tickets_users之前先旋轉glip_tickets_users,這樣你就不會有多個連接給你交叉連接,就像結果一樣。 – xQbert
因此,每張票可以有多個用戶,它就像一個按類型編號的鏈?即類型1是受讓人類型2的申請人,類型2本身是否是受讓人類型3的申請人?至少這是我理解你的查詢的方式。您能否在票證中至少顯示三位人員的樣本數據以及期望的結果? –