我有 6 个表:用户、日志、文件、标志、flag_details
users (id, avatar, fullname)
journals (id, container, container_id, user_id, title)
files (id, container, container_id, user_id, title)
flags (id, container, container_id, total)
flag_details (id, container, container_id, reason, comment, user_id)
trend_item (id, container, container_id, user_id)
关系是:
if(flags.container='look') then flags.container_id = journals.id
所以我从 journals 表中取出所有记录
if(flags.container='photo') then flags.container_id = files.id
所以我从文件表中获取所有记录
if(flags.container='trend') then flags.container_id = trend_item.id
所以我从trend_item 表中获取所有记录
所以我的观点是这样的:
id | container | things | total flag
1 | look | 123 - guy | 2
2 | photo | 321 - budi | 4
3 | trend | 345 - elvis | 3
4 | look | 876 - cans | 6
所以我得到了这样的查询:
SELECT DISTINCT (id), created_at, container, IF( container = 'look', (
SELECT a.container_id
FROM flags a, journals b
WHERE a.container_id = b.id
), IF( container = 'photo', (
SELECT a.container_id
FROM flags a, files b
WHERE a.container_id = b.id
), (
SELECT a.container_id
FROM flags a, trend_items b
WHERE a.container_id = b.id
) ) ) , total,
`status`
FROM flags
ORDER BY created_at DESC
但答案是子查询返回超过 1 行。
如果所有这些都放在一个查询中,有可能吗?