这个查询在三个查找表ls、lo和ld的帮助下从一个主表m、三个辅助表s、o和d中获得了我想要的结果,但是它为每个字段查询每个表一次。
SELECT m.id,
(SELECT GROUP_CONCAT(s.field1 SEPARATOR ', ')
FROM s, ls
WHERE ls.mid=m.id AND ls.sid=s.id)
AS s_field,
(SELECT GROUP_CONCAT(o.field1 SEPARATOR ', ')
FROM o, lo
WHERE lo.mid=m.id AND lo.oid=o.id)
AS o_field1,
(SELECT GROUP_CONCAT(o.field2 SEPARATOR ', ')
FROM o, lo
WHERE lo.mid=m.id AND lo.oid=o.id)
AS o_field2,
(SELECT GROUP_CONCAT(o.field3 SEPARATOR ', ')
FROM o, lo
WHERE lo.mid=m.id AND lo.oid=o.id)
AS o_field3,
(SELECT GROUP_CONCAT(o.field4 SEPARATOR ', ')
FROM o, lo
WHERE lo.mid=m.id AND lo.oid=o.id)
AS o_field4,
(SELECT GROUP_CONCAT(d.field1 SEPARATOR ', ')
FROM d, ld
WHERE ld.mid=m.id AND ld.did=d.id)
AS d_field1,
(SELECT GROUP_CONCAT(d.field2 SEPARATOR ', ')
FROM d, ld
WHERE ld.mid=m.id AND ld.did=d.id)
AS d_field2
FROM m
WHERE m.id=22 GROUP BY m.id
如何重写它以获得相同的结果,但每个表只有一个子查询。