Database: MySql 5.5.3
I have a type column in table topic, if the type is ‘E’ which means there is an entry exist in ‘Event’ Table and it should query “TOPIC_HAS_EVENT” table to get few more colums from the EVENT Table. In the same way if Type is ‘P’ which means there is an entry exist in ‘Poll Table’ and it should query “TOPIC_HAS_POLL” table to get few columns from POLL Table. If the type is null then it should query as usual to get all the rows and their comments from the comment table.
The end results should have all the topics rows of all types (‘E’,’P’, NULL) and their specific columns from other table. If that can accomplish easily then i need to add few more columns as mentioned below
More complex
If type=’E’ have some rows, it should get some more rows such as user information from EVENT_HAS_USER table.
Relationship
Each Topic has Many Comments
Each Topic has One Event
Each Topic has One Poll
EVENT ManyTOMany User
Query so far created. I still need to add ‘type’ column. Please help me with it.
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
Try this: