数据库:MySql 5.5.3
我在表type
中有topic
列,如果type
为'E',则表示“事件”表中存在条目,并且应该查询“TOPIC_HAS_EVENT”从EVENT表中获取更多colums的表格。同样,如果Type为'P',这意味着'Poll Table'中存在一个条目,它应该查询“TOPIC_HAS_POLL”表以从POLL表中获得几列。如果类型为null,那么它应该像往常一样查询从注释表中获取所有行及其注释。
最终结果应包含所有类型的所有主题行('E','P',NULL)及其他表中的特定列。如果可以轻松完成,那么我需要添加更多列,如下所述
如果type ='E'有一些行,它应该会获得更多行,例如来自EVENT_HAS_USER表的用户信息。
Each Topic has Many Comments
Each Topic has One Event
Each Topic has One Poll
EVENT ManyTOMany User
SELECT DISTINCT
T.TOPIC_GUID, COUNT(*) TOTAL_COMMENTS
FROM
CIRCLE C, CIRCLE_HAS_USER CHU, CIRCLE_HAS_TOPIC CHT, TOPIC T
LEFT JOIN TOPIC_COMMENT TC ON T.TOPIC_GUID = TC.TOPIC_GUID
WHERE
CHT.CIRCLE_GUID = C.CIRCLE_GUID
AND T.TOPIC_GUID < 400000 -- ?
AND CHT.TOPIC_GUID = T.TOPIC_GUID
AND CHU.CIRCLE_GUID = C.CIRCLE_GUID
AND CHU.USER_GUID = 1
AND CHU.STATUS = 'A'
GROUP BY T.TOPIC_GUID
ORDER BY T.LAST_UPDATED_TIMESTAMP DESC
LIMIT 10
答案 0 :(得分:1)
试试这个:
select (case when type = 'type1' then (select field from table1) else (select field from table2) end) as a from table;