我有一个带有多个子查询的复杂mysql查询。我需要其中一个子查询与主表相关。那是我的疑问:
SELECT table_cl.id, table_cl.nombre, table_cl.apellidos, (SELECT COUNT(*) FROM (
SELECT iauno.id_accion, iauno.id_cliente, iauno.asistencia
FROM inter_clientes_acciones iauno
INNER JOIN clientes clientesuno ON clientesuno.id = iauno.id_cliente
WHERE iauno.id_cliente IN(
SELECT iasubuno.id_cliente
FROM inter_clientes_acciones iasubuno
INNER JOIN acciones acsubuno ON iasubuno.id_accion = acsubuno.id
WHERE acsubuno.id_proyecto = 3
GROUP BY iasubuno.id_accion
HAVING COUNT(*) = 1
)
)caa
) as convocado,
(SELECT COUNT(*) FROM (
SELECT iados.id_accion, iados.id_cliente, iados.asistencia
FROM inter_clientes_acciones iados
INNER JOIN clientes cldos ON cldos.id = iados.id_cliente
WHERE iados.id_cliente IN(
SELECT iasubdos.id_cliente
FROM inter_clientes_acciones iasubdos
INNER JOIN acciones acsubdos ON iasubdos.id_accion = acsubdos.id
WHERE acsubdos.id_proyecto = 3 AND iasubdos.id_cliente = table_cl.id
GROUP BY id_accion
HAVING COUNT(*) = 1
)
)car
) as realizado
FROM clientes table_cl
WHERE table_cl.id_proyecto = 3 AND table_cl.id IN (
SELECT iatres.id_cliente
FROM inter_clientes_acciones iatres
INNER JOIN clientes cltres ON cltres.id = iatres.id_cliente
WHERE iatres.id_cliente IN(
SELECT iasubtres.id_cliente
FROM inter_clientes_acciones iasubtres
INNER JOIN acciones acsubtres ON iasubtres.id_accion = acsubtres.id
WHERE acsubtres.id_proyecto = 3
GROUP BY iasubtres.id_accion
HAVING COUNT(*) = 1
)
)
当我执行查询时,出现错误Error Code: 1054. Unknown column 'table_cl.id' in 'where clause'
我如何修改查询以引用table_cl上的id
修改
可能需要解释:我有3个表,客户端,操作和clients_actions(用西班牙语表示:clientes,acciones和inter_clientes_acciones)。可以为一个客户端或多个客户端创建一个操作。此查询的目的是获取以下字段:客户端的名称和姓氏,分配给客户端的操作数,以及客户端已协助的此操作的数量,但仅指定分配给他的操作(单个操作)。 / p>
这里有一个sql fiddle,其中包含架构和我所做的失败的SQL。
- >编辑2: 我找到了另一种获取数据的方法。在这种情况下,我将字段中的所有操作ID分组,并且(在PHP文件中)我将其计算在内。我收到了这个回复:
Individual actions: [ { "nombre": "Susana", "apellidos": "Rodr\u00edguez Torr\u00f3n", "acc_convocadas": "2,3,5", "acc_realizadas": "2" }, { "nombre": "Pablo", "apellidos": "Campos P\u00e9rez", "acc_convocadas": "5", "acc_realizadas": "-" }, { "nombre": "Oscar", "apellidos": "Delacroix", "acc_convocadas": "-", "acc_realizadas": "-" } ]
Group actions: [ { "nombre": "Susana", "apellidos": "Rodr\u00edguez Torr\u00f3n", "acc_convocadas": "3,1,4", "acc_realizadas": "-" }, { "nombre": "Pablo", "apellidos": "Campos P\u00e9rez", "acc_convocadas": "-", "acc_realizadas": "-" }, { "nombre": "Oscar", "apellidos": "Delacroix", "acc_convocadas": "3", "acc_realizadas": "-" } ]
这是
条款SELECT nombre, apellidos,
IFNULL((SELECT GROUP_CONCAT(id_accion)
FROM inter_clientes_acciones iac
WHERE iac.id_cliente = cl.id AND id_accion IN (
SELECT iauno.id_accion
FROM inter_clientes_acciones iauno
INNER JOIN acciones acuno ON iauno.id_accion = acuno.id
WHERE acuno.id_proyecto = 3
GROUP BY iauno.id_accion
HAVING COUNT(*) > 1
)), "-") as acc_convocadas,
IFNULL((SELECT GROUP_CONCAT(id_accion)
FROM inter_clientes_acciones iac
WHERE iac.id_cliente = cl.id AND asistencia = 1 AND id_accion IN (
SELECT iauno.id_accion
FROM inter_clientes_acciones iauno
INNER JOIN acciones acuno ON iauno.id_accion = acuno.id
WHERE acuno.id_proyecto = 3
GROUP BY iauno.id_accion
HAVING COUNT(*) > 1)), "-") as acc_realizadas
FROM clientes cl
WHERE id_proyecto = 3
答案 0 :(得分:0)
应该可以将table_cl.id的检查从子查询的最低级别移到更高级别,在那里(希望)MySQL会识别它。但是我有点担心子查询如何与id_accion分组的方式一起工作,而不会让它回归。使跟踪变得困难。
但是,最佳猜测是table_cl.id的检查位于子查询的最高级别。但是,如果没有表格声明,它是否真的可以进行任何测试。
SELECT table_cl.id, table_cl.nombre, table_cl.apellidos, (SELECT COUNT(*) FROM (
SELECT iauno.id_accion, iauno.id_cliente, iauno.asistencia
FROM inter_clientes_acciones iauno
INNER JOIN clientes clientesuno ON clientesuno.id = iauno.id_cliente
WHERE iauno.id_cliente IN(
SELECT iasubuno.id_cliente
FROM inter_clientes_acciones iasubuno
INNER JOIN acciones acsubuno ON iasubuno.id_accion = acsubuno.id
WHERE acsubuno.id_proyecto = 3
GROUP BY iasubuno.id_accion
HAVING COUNT(*) = 1
)
)caa
) as convocado,
(
SELECT COUNT(*)
FROM inter_clientes_acciones iados
INNER JOIN clientes cldos ON cldos.id = iados.id_cliente
INNER JOIN
(
SELECT iasubdos.id_cliente,
id_accion
FROM inter_clientes_acciones iasubdos
INNER JOIN acciones acsubdos ON iasubdos.id_accion = acsubdos.id
WHERE acsubdos.id_proyecto = 3
GROUP BY iasubdos.id_cliente, id_accion
HAVING COUNT(*) = 1
) sub0 ON iados.id_cliente = sub0.id_cliente
WHERE iados.id_cliente = table_cl.id
) as realizado
FROM clientes table_cl
WHERE table_cl.id_proyecto = 3 AND table_cl.id IN (
SELECT iatres.id_cliente
FROM inter_clientes_acciones iatres
INNER JOIN clientes cltres ON cltres.id = iatres.id_cliente
WHERE iatres.id_cliente IN(
SELECT iasubtres.id_cliente
FROM inter_clientes_acciones iasubtres
INNER JOIN acciones acsubtres ON iasubtres.id_accion = acsubtres.id
WHERE acsubtres.id_proyecto = 3
GROUP BY iasubtres.id_accion
HAVING COUNT(*) = 1
)
)
编辑
我已将其清理干净,但无法使用您的测试数据对其进行测试。我也把表名改为英语,这样我才能理解它们!
但是它确实执行了,而且我认为它符合你原本要做的事情。我觉得有可能进一步清理它,但我累了,感冒了,已经晚了!
SELECT table_cl.id,
table_cl.nombre,
table_cl.apellidos,
(
SELECT COUNT(*)
FROM client_actions
INNER JOIN
(
SELECT DISTINCT client_actions.id_cliente
FROM client_actions
INNER JOIN actions ON client_actions.id_accion = actions.id
WHERE actions.id_proyecto = 3
GROUP BY client_actions.id_accion
HAVING COUNT(*) = 1
) sub1
ON client_actions.id_cliente = sub1.id_cliente
) as convocado,
(
SELECT COUNT(*)
FROM client_actions
INNER JOIN
(
SELECT DISTINCT client_actions.id_cliente
FROM client_actions
INNER JOIN actions ON client_actions.id_accion = actions.id
WHERE actions.id_proyecto = 3
GROUP BY id_accion
HAVING COUNT(*) = 1
) sub2
ON client_actions.id_cliente = sub2.id_cliente
WHERE client_actions.id_cliente = table_cl.id
) as realizado
FROM clients table_cl
INNER JOIN
(
/* Get all the clients who have been the sole client for an action */
SELECT DISTINCT client_actions.id_cliente
FROM client_actions
INNER JOIN actions ON client_actions.id_accion = actions.id
WHERE actions.id_proyecto = 3
GROUP BY client_actions.id_accion
HAVING COUNT(*) = 1
) sub0
ON table_cl.id = sub0.id_cliente
WHERE table_cl.id_proyecto = 3