在JOIN中使用相同的表两次“未知列”错误

时间:2016-04-26 17:25:11

标签: mysql join

我有以下查询:

SELECT 
    z.id,users.name,e.name,tt.date,
    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,
    ROUND(SUM(tt.actiontime/3600),2) AS HORAS, c.name, i.name, 
    glpi_tickets_users.type 
FROM glpi_tickettasks tt 
INNER JOIN glpi_tickets z ON tt.tickets_id = z.id 
LEFT JOIN glpi_itilcategories i ON z.itilcategories_id = i.id 
INNER JOIN glpi_users AS users  ON users.id = tt.users_id 
INNER JOIN glpi_users AS req  ON req.id = glpi_tickets_users.users_id 
LEFT JOIN glpi_usercategories c ON c.id = users.usercategories_id 
INNER JOIN glpi_entities e ON z.entities_id = e.id
WHERE  tt.date>'2016-01-06'   and tt.date<'2016-01-06' 
GROUP  BY tt.users_id,tt.id  
ORDER BY users.name limit 5 

它输出以下错误:

  

错误1054(42S22)第1行:未知列'glpi_tickets_users.type'   在'字段列表'

我需要两次使用表glpi_users来获取“用户类型”。

我的查询有什么问题?

4 个答案:

答案 0 :(得分:3)

那是因为您的查询中没有列出表格glpi_tickets_users的位置。无论是from条款还是join条款中的任何条款。如果您不想要该列glpi_tickets_users.type,那么只需将其从查询中删除即可正常使用。

答案 1 :(得分:2)

到目前为止,您还没有在查询中加入表格glpi_tickets_users。

SELECT z.id,users.name,e.name,tt.date,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,ROUND(SUM(tt.actiontime/3600),2) AS HORAS,c.name,i.name,glpi_tickets_users.type 
FROM glpi_tickettasks tt 
INNER JOIN glpi_tickets z ON tt.tickets_id = z.id 
LEFT JOIN glpi_itilcategories i ON z.itilcategories_id = i.id 
INNER JOIN glpi_users AS users  ON users.id = tt.users_id 
/**here is the join to glpi_tickets_users **/
INNER JOIN glpi_tickets_users ON users.id = glpi_tickets_users.users_id
INNER JOIN glpi_users AS req  ON req.id = glpi_tickets_users.users_id 
LEFT JOIN glpi_usercategories c ON c.id = u.usercategories_id 
INNER JOIN glpi_entities e ON z.entities_id = e.id
WHERE  tt.date>'2016-01-06'   and tt.date<'2016-01-06'  GROUP  BY tt.users_id,tt.id  ORDER BY users.name limit 5

答案 2 :(得分:1)

我不确定表的数据结构,但不是glpi_users表上的type列?

如果是这种情况,请将glpi_tickets_users.type替换为glpi_users.type

最终的查询将是:

SELECT z.id,users.name,e.name,tt.date,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,ROUND(SUM(tt.actiontime/3600),2) AS HORAS,c.name,i.name,glpi_users.type

FROM glpi_tickettasks tt

INNER JOIN glpi_tickets z ON tt.tickets_id = z.id

LEFT JOIN glpi_itilcategories i ON z.itilcategories_id = i.id

INNER JOIN glpi_users AS users  ON users.id = tt.users_id

INNER JOIN glpi_users AS req  ON req.id = glpi_tickets_users.users_id

LEFT JOIN glpi_usercategories c ON c.id = u.usercategories_id

INNER JOIN glpi_entities e ON z.entities_id = e.id  

WHERE  tt.date>'2016-01-06'   and tt.date<'2016-01-06'  GROUP  BY tt.users_id,tt.id  ORDER BY users.name limit 5 

答案 3 :(得分:1)

您根本没有在查询中加入表 glpi_tickets_users ,因此您无法从中进行选择。您需要修改查询以包含 glpi_tickets_users