I have this query which adds a load of values to a 'stats' table. When the query runs it selects the values to fill the table in subqueries. I was wondering if this could be made any more efficiently or if I am doing something really wrong. I am not that familiar with MySQL so any help would be great :)
Here is the query:
UPDATE mediastats SET
mediastats_members = (SELECT count(*) FROM status WHERE status_media_id = :id),
mediastats_avscore = (SELECT AVG(status_rating) FROM status WHERE status_media_id = :id),
mediastats_done = (SELECT count(*) FROM status WHERE status_status = 'done' AND status_media_id = :id),
mediastats_doing = (SELECT count(*) FROM status WHERE status_status = 'doing' AND status_media_id = :id),
mediastats_redoing = (SELECT count(*) FROM status WHERE status_status = 'redoing' AND status_media_id = :id),
mediastats_dropped = (SELECT count(*) FROM status WHERE status_status = 'dropped' AND status_media_id = :id),
mediastats_wantto = (SELECT count(*) FROM status WHERE status_status = 'wantto' AND status_media_id = :id),
mediastats_wont = (SELECT count(*) FROM status WHERE status_status = 'wont' AND status_media_id = :id),
mediastats_stalled = (SELECT count(*) FROM status WHERE status_status = 'stalled' AND status_media_id = :id),
mediastats_rating_1 = (SELECT count(*) FROM status WHERE status_rating = 1 AND status_media_id = :id),
mediastats_rating_2 = (SELECT count(*) FROM status WHERE status_rating = 2 AND status_media_id = :id),
mediastats_rating_3 = (SELECT count(*) FROM status WHERE status_rating = 3 AND status_media_id = :id),
mediastats_rating_4 = (SELECT count(*) FROM status WHERE status_rating = 4 AND status_media_id = :id),
mediastats_rating_5 = (SELECT count(*) FROM status WHERE status_rating = 5 AND status_media_id = :id),
mediastats_rating_6 = (SELECT count(*) FROM status WHERE status_rating = 6 AND status_media_id = :id),
mediastats_rating_7 = (SELECT count(*) FROM status WHERE status_rating = 7 AND status_media_id = :id),
mediastats_rating_8 = (SELECT count(*) FROM status WHERE status_rating = 8 AND status_media_id = :id),
mediastats_rating_9 = (SELECT count(*) FROM status WHERE status_rating = 9 AND status_media_id = :id),
mediastats_rating_10 = (SELECT count(*) FROM status WHERE status_rating = 10 AND status_media_id = :id)
WHERE mediastats_media_id = :id
The :id is added from PHP.
Here's how I'd do it in PHP with PDO:
$sql = "
SELECT
COUNT(*) AS mediastats_members,
AVG(status_rating) AS mediastats_avscore,
SUM(status_status = 'done') AS mediastats_done,
SUM(status_status = 'doing') AS mediastats_doing,
SUM(status_status = 'redoing') AS mediastats_redoing,
SUM(status_status = 'dropped') AS mediastats_dropped,
SUM(status_status = 'wantto') AS mediastats_wantto,
SUM(status_status = 'wont') AS mediastats_wont,
SUM(status_status = 'stalled') AS mediastats_stalled,
SUM(status_rating = 1) AS mediastats_rating_1,
SUM(status_rating = 2) AS mediastats_rating_2,
SUM(status_rating = 3) AS mediastats_rating_3,
SUM(status_rating = 4) AS mediastats_rating_4,
SUM(status_rating = 5) AS mediastats_rating_5,
SUM(status_rating = 6) AS mediastats_rating_6,
SUM(status_rating = 7) AS mediastats_rating_7,
SUM(status_rating = 8) AS mediastats_rating_8,
SUM(status_rating = 9) AS mediastats_rating_9,
SUM(status_rating = 10) AS mediastats_rating_10
FROM status
WHERE status_media_id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute(array("id"=>$id));
$params = $stmt->fetch(PDO::FETCH_ASSOC);
This way you calculate all the aggregates in one pass of the table, instead of using a separate subquery for each count.
I'm using a trick of MySQL -- the SUM() of a boolean expression is equal to the COUNT() where the expression is true. This is because MySQL boolean expressions always return 0 or 1, and the SUM of 0's and 1's is equal to the COUNT of the 1's.
Then you can use the result from the above query as the parameters array to an UPDATE statement:
$sql = "
UPDATE mediastats SET
mediastats_members = :mediastats_members,
mediastats_avscore = :mediastats_avscore,
mediastats_done = :mediastats_done,
mediastats_doing = :mediastats_doing,
mediastats_redoing = :mediastats_redoing,
mediastats_dropped = :mediastats_dropped,
mediastats_wantto = :mediastats_wantto,
mediastats_wont = :mediastats_wont,
mediastats_stalled = :mediastats_stalled,
mediastats_rating_1 = :mediastats_rating_1,
mediastats_rating_2 = :mediastats_rating_2,
mediastats_rating_3 = :mediastats_rating_3,
mediastats_rating_4 = :mediastats_rating_4,
mediastats_rating_5 = :mediastats_rating_5,
mediastats_rating_6 = :mediastats_rating_6,
mediastats_rating_7 = :mediastats_rating_7,
mediastats_rating_8 = :mediastats_rating_8,
mediastats_rating_9 = :mediastats_rating_9,
mediastats_rating_10 = :mediastats_rating_10
WHERE mediastats_media_id = :id";
$stmt = $pdo->prepare($sql);
$params["id"] = $id;
$stmt->execute($params);
Since PHP 5.3.4, PDO accepts parameter array keys without the leading :. You need the colon when you declare the parameter placeholder in your query, but you don't need it in the array of values you supply to execute().
UPDATE (
SELECT status_media_id,
COUNT(*) AS cnt, AVG(status_rating) AS avg_rating,
SUM(status_status = 'done') AS cnt_done,
...
FROM status
WHERE status_media_id = :id
) s
JOIN mediastats ms
ON ms.mediastats_media_id = s.status_media_id
SET ms.mediastats_members = cnt,
ms.mediastats_avscore = avg_rating,
ms.mediastats_done = cnt_done,
...
Related
SELECT o.* FROM ( SELECT
#rownum := #rownum + 1 RN,
i.* FROM<br>
(
SELECT
#rownum:=0
)
r,<br>
(
SELECT DISTINCT<br>
CG_MGR_USR.MANAGER_NAME,<br>
CG_MGR_USR.PARAM_NAME,<br>
USR_TR_DATA.USER_ID,<br>
CG_MGRS.FIRST_NAME,<br>
CG_MGRS.LAST_NAME,<br>
CC_GENOBJ.OBJNAME,<br>
USR_TR_DATA.RISK_ID,<br>
USR_TR_DATA.VERSION_ID,<br>
T_RISK.RISK_DESCR,<br>
CC_RISKT.RISK_ID CC_RISK,<br>
CC_RISKT.DESCN,<br>
USR_TR_DATA.SYSTEM_ID,<br>
T_STATUS.STATUS,<br>
USR_TR_DATA.COUNTS,<br>
SYSTEMS.SYSTEM_NAME<br>
FROM
TABLE_USR_TR_DATA USR_TR_DATA<br>
LEFT OUTER JOIN TABLE_CC_GENOBJ CC_GENOBJ<br>
ON
CC_GENOBJ.GENOBJTP = 1
AND CC_GENOBJ.SYSTEM_ID IN<br>
(
SELECT
sys_id
FROM
TABLE_CS_SYSTEM
WHERE
cs_sys_id =USR_TR_DATA.system_id
AND is_primary='Y'
)<br>
AND CC_GENOBJ.GENOBJID = USR_TR_DATA.USER_ID,<br>
TABLE_RISK T_RISK<br>
LEFT OUTER JOIN TABLE_CC_RISKT CC_RISKT
ON<br>
CC_RISKT.LANG = 'EN'
AND T_RISK.CC_RISK_ID = CC_RISKT.RISK_ID,<br>
TABLE_CG_MGR_USR CG_MGR_USR,<br>
TABLE_CG_MGRS CG_MGRS,<br>
TABLE_STATUS T_STATUS,<br>
TABLE_CS_SYSTEM CS_SYSTEMS,<br>
TABLE_SYSTEMS SYSTEMS<br>
WHERE
USR_TR_DATA.RISK_ID = T_RISK.RISK_ID<br>
AND USR_TR_DATA.VERSION_ID = T_RISK.VERSION_ID<br>
AND CG_MGR_USR.USER_NAME = USR_TR_DATA.USER_ID<br>
AND CG_MGR_USR.SYSTEM_ID = CS_SYSTEMS.SYS_ID<br>
AND CG_MGRS.MANAGER_NAME = CG_MGR_USR.MANAGER_NAME<br>
AND CG_MGRS.PARAM_NAME =CG_MGR_USR.PARAM_NAME<br>
AND CG_MGRS.SYSTEM_ID = CG_MGR_USR.SYSTEM_ID<br>
AND CS_SYSTEMS.CS_SYS_ID = USR_TR_DATA.SYSTEM_ID<br>
AND CS_SYSTEMS.IS_PRIMARY = 'Y'<br>
AND T_STATUS.SEQ_NO = USR_TR_DATA.STATUS_ID<br>
AND SYSTEMS.SYSTEMS_ID = USR_TR_DATA.SYSTEM_ID<br>
AND CG_MGR_USR.MANAGER_NAME IN( 'SAPUSER' )<br>
AND T_STATUS.STATUS IN( 'IN-PROCESS', 'OPEN' )<br>
AND USR_TR_DATA.COUNTS >= 1<br>
ORDER BY
USER_ID,
SYSTEM_ID,
RISK_ID,
VERSION_ID,
CC_RISK,
STATUS,
COUNTS ASC
)
i ) o <br>WHERE o.RN >= 1 AND o.RN <= 10
It is taking too much time for to fetch only 10 records.
I want to make my report faster because is taking to long to load, i was wondering if there is a way to make multiple counts from just one select.
Example, make this select wish is not going to change for the counts
SELECT IDS, Fecha_Recarga, Banco_Recarga, Tipo, Status, RefVerif, MontoVerif, RecargaDuplicada FROM transaccionesrr WHERE Fecha_Recarga = '2017-02-07' AND Banco_Recarga = 'BANESCO' AND Tipo = 'RECARGA' AND Status = 'PROCESADA'
And based on the result of this select i want to make multiple counts like
Count (*) WHERE RefVerif = '1' AS RefVerif
Count (*) WHERE RefVerif = '1' AND MontoVerif = '0' AS MontoVerif
Count (*) WHERE RecargaDuplicada = '1' AS Duplicada
This is the Stored Procedure that i have right know
SELECT COUNT(*) AS RECARGAS,
(SELECT COUNT(*) FROM transaccionesrr WHERE Tipo = 'RECARGA' AND Status = 'PROCESADA' AND Fecha_Recarga BETWEEN PRM_Fecha_Desde AND PRM_Fecha_Hasta AND Banco_Recarga = PRM_Banco AND RefVerif = '1') AS VERIFICADAS,
(SELECT COUNT(*) FROM transaccionesrr WHERE Tipo = 'RECARGA' AND Status = 'PROCESADA' AND Fecha_Recarga BETWEEN PRM_Fecha_Desde AND PRM_Fecha_Hasta AND Banco_Recarga = PRM_Banco AND RefVerif = '0') AS NOVERIFICADAS,
(SELECT COUNT(*) FROM transaccionesrr WHERE Tipo = 'RECARGA' AND Status = 'PROCESADA' AND Fecha_Recarga BETWEEN PRM_Fecha_Desde AND PRM_Fecha_Hasta AND Banco_Recarga = PRM_Banco AND RefVerif = '1' AND MontoVerif = '0') AS MONTOVERIF,
(SELECT COUNT(*) FROM transaccionesrr WHERE Tipo = 'RECARGA' AND Status = 'PROCESADA' AND Fecha_Recarga BETWEEN PRM_Fecha_Desde AND PRM_Fecha_Hasta AND Banco_Recarga = PRM_Banco AND RecargaDuplicada = '1') AS DUPLICADAS
FROM transaccionesrr WHERE Tipo = 'RECARGA' AND Status = 'PROCESADA' AND Fecha_Recarga BETWEEN PRM_Fecha_Desde AND PRM_Fecha_Hasta AND Banco_Recarga = PRM_Banco ;
Im trying to do this because i think it will be faster if you think there is a better way i will be appreciated.
I have been trying to make a solution for days but i don't find any
Use conditional aggregation:
SELECT COUNT(*) AS RECARGAS,
SUM(RefVerif = '1') AS VERIFICADAS,
SUM(RefVerif = '0') AS NOVERIFICADAS,
SUM(RefVerif = '1' AND MontoVerif = '0') AS MONTOVERIF,
SUM(RecargaDuplicada = '1') AS DUPLICADAS
FROM transaccionesrr
WHERE Tipo = 'RECARGA' AND Status = 'PROCESADA' AND
Fecha_Recarga BETWEEN PRM_Fecha_Desde AND PRM_Fecha_Hasta AND
Banco_Recarga = PRM_Banco ;
I'm trying to return the row with ID 258776, here's what I've tried so far...
Here's my example, I use the AND msg.uid to only get one thread of messages. I want to return the latest post of each thread therefore in the example with just 2 messages for 1 thread, I want the row with ID 258776 to be returned.
Without GROUP BY:
SELECT main.id, main.message_id, main.inbox_id, main.uid, main.body, main.created_at FROM (
SELECT i.id as inbox_id, i.message_id, msg.* FROM inbox AS i
INNER JOIN message AS msg ON i.message_id = msg.id
WHERE i.profile_id = 2135
AND i.is_sent = 0
AND i.is_deleted = 0
#AND msg.uid = '570cc3a568402'
ORDER BY msg.updated_at DESC
) AS main #GROUP BY main.uid
= ID = =MSG = =INB = = UID = = BD = = CREATED =
258776 258776 524785 570cc3a568402 wtf 2016-06-22 11:34:29
217149 217149 438907 570cc3a568402 <br /> 2016-04-12 11:45:09
Try with GROUP BY
SELECT main.id, main.message_id, main.inbox_id, main.uid, main.body, main.created_at FROM (
SELECT i.id as inbox_id, i.message_id, msg.* FROM inbox AS i
INNER JOIN message AS msg ON i.message_id = msg.id
WHERE i.profile_id = 2135
AND i.is_sent = 0
AND i.is_deleted = 0
#AND msg.uid = '570cc3a568402'
ORDER BY msg.updated_at DESC
) AS main GROUP BY main.uid
= ID = =MSG = =INB = = UID = = BD = = CREATED =
217149 217149 438907 570cc3a568402 <br /> 2016-04-12 11:45:09
Switch to ASC but GROUP BY gives same results ?
SELECT main.id, main.message_id, main.inbox_id, main.uid, main.body, main.created_at FROM (
SELECT i.id as inbox_id, i.message_id, msg.* FROM inbox AS i
INNER JOIN message AS msg ON i.message_id = msg.id
WHERE i.profile_id = 2135
AND i.is_sent = 0
AND i.is_deleted = 0
#AND msg.uid = '570cc3a568402'
ORDER BY msg.updated_at ASC
) AS main GROUP BY main.uid
= ID = =MSG = =INB = = UID = = BD = = CREATED =
217149 217149 438907 570cc3a568402 <br /> 2016-04-12 11:45:09
I presume it should work fine if I didn't need to use an INNER join ? :(
EDIT:
Here's some more data from the message table and inbox table.
message
= ID = =FROMID= = UID = = TITLE = = BODY = = CREATED =
258776 52169 570cc3a568402 RE: RE: wtf 2016-06-22 11:34:29
258775 2135 570cc3a568402 RE: You Testtest 2016-06-22 11:31:29
258774 34833 576a590fdf9e5 RE: Sure < 3 < 3 2016-06-22 11:24:08
258773 34833 576a590fdf9e5 RE: Sure sok 2016-06-22 11:23:57
258772 34833 576a590fdf9e5 RE: Sure hey hey 2016-06-22 11:23:46
inbox
= ID = = PROFILE = = MSG_ID = = IS_SENT = = IS_READ = = IS_DELETED =
524785 2135 258776 0 0 0
524784 52169 258776 1 1 0
524783 52169 258775 0 1 0
524782 2135 258775 1 0 0
524781 2135 258774 0 1 0
If you want the max id that match you condition you should use
SELECT main.id, main.message_id, main.inbox_id, main.uid, main.body, main.created_at
FROM (
SELECT i.id as inbox_id, i.message_id, msg.* FROM inbox AS i
INNER JOIN message AS msg ON i.message_id = msg.id
WHERE i.id = (
SELECT max(i.id) FROM inbox AS i
INNER JOIN message AS msg ON i.message_id = msg.id
WHERE i.profile_id = 2135
AND i.is_sent = 0
AND i.is_deleted = 0
AND msg.uid = '570cc3a568402'
)
) AS main
Group by like said in comment are for aggegatte function (with multiplr result) you don't have this then you don't need
I ended up using PHP to solve my problem, by performing a PDO query to select all the messages, then loop through these results to have a simple array of message IDs grouped by the thread ID (uid)...
$pdo = Doctrine_Manager::connection();
$results = $pdo->execute('SELECT i.id, i.message_id, msg.uid FROM inbox AS i
INNER JOIN message AS msg ON i.message_id = msg.id
WHERE i.profile_id = :profile_id
AND i.is_sent = :is_sent
AND i.is_deleted = :is_deleted
ORDER BY msg.created_at ASC', [
':profile_id' => $address->getId(),
':is_sent' => 0,
':is_deleted' => 0,
])->fetchAll();
$whereIn = [];
foreach ($results as $key => $value) {
$whereIn[$value['uid']] = $value['id'];
}
return $this
->getBaseMessageBoxQuery()
->addWhere('i.profile_id = ?', $address->getId())
->andWhere('i.is_sent = 0')
->andWhere('i.is_deleted = 0')
->andWhereIn('i.id', array_values($whereIn))
->addGroupBy('msg.uid')
->orderBy('msg.created_at DESC');
To reiterate my original comment; unless there is some unusual logic you are trying to implement, I really don't see a need for either the GROUP BY or the subquery; just use LIMIT:
SELECT msg.id, i.message_id, i.id as inbox_id, msg.uid, msg.body, msg.created_at
FROM inbox AS i
INNER JOIN message AS msg ON i.message_id = msg.id
WHERE i.profile_id = 2135
AND i.is_sent = 0
AND i.is_deleted = 0
AND msg.uid = '570cc3a568402'
ORDER BY msg.updated_at DESC
LIMIT 1
EDIT: With the additional information, this should allow you to get what you want with just a query:
SELECT m.id, i.message_id, i.id as inbox_id, m.uid, m.body, m.created_at
FROM
(SELECT uid, MAX(updated_at) AS updated_at
FROM message
GROUP BY uid) AS lastMs
INNER JOIN message AS m USING(uid, updated_at)
INNER JOIN inbox AS i ON m.id = i.message_id
The primary concern I can see with this query, or any possible solution, with the problem as it has been described so far is that it is possible there may be multiple messages with the same uid AND updated_at; in which case, multiple results would be returned for that uid.
I have the following query and I would like to make it so if there's a duplicate key it updates the values
INSERT INTO totalData (pageId, dateScanned, totalPageLikes, totalTalkingAbout, totalPos, totalNeg, totalFemales, totalMales, totalStrongPositives, totalPositives, totalWeakPositives, totalNeutrals, totalWeakNegatives, totalNegatives, totalStrongNegatives, totalStatuses, totalStatusLikes, totalStatusShares, totalComments, totalUniqueCommenters)
SELECT pages.pageId, pages.dateScanned, pages.likes, pages.talkingAbout,
SUM(commentTags.tag LIKE '%positive%') AS positive,
SUM(commentTags.tag LIKE '%negative%') AS negative,
SUM(comments.gender = 'female') AS females,
SUM(comments.gender = 'male') AS males,
SUM(commentTags.tag = 'strong_positive') AS strongPositives,
SUM(commentTags.tag = 'positive') AS positives,
SUM(commentTags.tag = 'weak_positive') AS weakPositives,
SUM(commentTags.tag = 'neutral') AS neutrals,
SUM(commentTags.tag = 'weak_negative') AS weakNegatives,
SUM(commentTags.tag = 'negative') AS negatives,
SUM(commentTags.tag = 'strong_negative') AS strongNegatives,
COUNT(DISTINCT statuses.statusId) AS totalStatuses,
SUM(DISTINCT statuses.likesCount) AS totalLikesCount,
SUM(DISTINCT statuses.sharesCount) AS totalSharesCount,
COUNT(DISTINCT comments.commentId) AS totalComments,
COUNT(DISTINCT comments.userName) AS uniqueUsers
FROM pages
JOIN statuses ON pages.pageId = statuses.pageId AND pages.dateScanned = statuses.dateScanned
JOIN comments ON comments.statusID = statuses.statusId
JOIN commentTags ON comments.commentId = commentTags.commentId
WHERE pages.pageId = '115798033817' AND pages.dateScanned = '2013-11-05'
I tried the ON DUPLICATE KEY UPDATE and this is how I further modified the query
ON DUPLICATE KEY UPDATE
totalData.pageId = pageId, totalData.dateScanned = dateScanned,
totalData.totalPageLikes = totalPageLikes, totalData.totalTalkingAbout = totalTalkingAbout,
totalData.totalPos = positive, totalData.totalNeg = negative, totalData.totalFemales = females,
totalData.totalMales = males, totalData.totalStrongPositives = strongPositives,
totalData.totalPositives = positives, totalData.totalWeakPositives = weakPositives,
totalData.totalNeutrals = neutrals, totalData.totalWeakNegatives = weakNegatives,
totalData.totalNegatives = negatives, totalData.totalStrongNegatives = strongNegatives,
totalData.totalStatuses = totalStatuses, totalData.totalStatusLikes = totalLikesCount,
totalData.totalStatusShares = totalSharesCount, totalData.totalComments = totalComments,
totalData.totalUniqueCommenters = uniqueUsers ;
But when i run the query it says Unknown column 'positive' in field list.
Got it, I had to use the VALUES() function so the ON DUPLICATE KEY UPDATE part of the query becomes like the following
ON DUPLICATE KEY UPDATE
totalPageLikes = VALUES(totalPageLikes), totalTalkingAbout = VALUES(totalTalkingAbout),
totalPos = VALUES(totalPos), totalNeg = VALUES(totalNeg), totalFemales = VALUES(totalFemales), totalMales = VALUES(totalMales),
totalStrongPositives = VALUES(totalStrongPositives), totalPositives = VALUES(totalPositives), totalWeakPositives = VALUES(totalWeakPositives),
totalNeutrals = VALUES(totalNeutrals), totalWeakNegatives = VALUES(totalWeakNegatives), totalNegatives = VALUES(totalNegatives), totalStrongNegatives = VALUES(totalStrongNegatives),
totalStatuses = VALUES(totalStatuses), totalStatusLikes = VALUES(totalStatusLikes), totalStatusShares = VALUES(totalStatusShares),
totalComments = VALUES(totalComments), totalUniqueCommenters = VALUES(totalUniqueCommenters) ";
Hi I am doing a union over several tables. It's a little long but works!
(SELECT user_id,added_date,group_id,'joined',0,0,'' FROM group_members WHERE status = 1)
UNION
(SELECT user_id,added_date,object_id,'made a comment',0,0,'' FROM comments WHERE object_type = 11 AND status = 1)
UNION
(SELECT user_id,added_date,group_id,'made the event',1,group_calendar_id,title FROM group_calendars WHERE status = 1)
UNION
(SELECT comments.user_id,comments.added_date,group_calendars.group_id,'made a comment on the event',1,group_calendar_id,'' FROM group_calendars
INNER JOIN comments ON group_calendars.group_calendar_id = comments.object_id WHERE group_calendars.status = 1 AND comments.status = 1 AND object_type = 10
)
UNION
(SELECT user_id,pd.added_date,pd.object_id,'uploaded a photo',2,pd.photo_data_id,
(SELECT varchar_val FROM photo_data WHERE data_id = 1 AND photo_data.photo_id = photos.photos_id AND object_type = 3 AND object_id = pd.object_id)
FROM photo_data pd
INNER JOIN photos ON photos.photos_id = pd.photo_id
WHERE photos.photo_status = 1 AND pd.status = 1 AND pd.data_id = 0 AND pd.object_type = 3
)
UNION
(SELECT cp.user_id,cp.added_date,cp.object_id,'made a comment on the photo',2,pd.photo_data_id,
(SELECT varchar_val FROM photo_data WHERE data_id = 1 AND photo_data.photo_id = photos.photos_id AND object_type = 3 AND object_id = pd.object_id)
FROM comments cp
INNER JOIN photo_data pd ON pd.photo_data_id = cp.object_id
INNER JOIN photos ON photos.photos_id = pd.photo_id
WHERE cp.object_type = 8 AND cp.status = 1 AND pd.status = 1 AND pd.data_id = 0 AND photos.photo_status = 1 AND pd.object_type = 3
)
UNION
(SELECT user_id,added_date,group_id,'made a topic',3,forum_topic_id,title FROM forum_topics WHERE forum_categories_id = ".GROUP_FORUM_CATEGORY." AND group_id > 0 AND status = 1)
UNION
(SELECT forum_comments.user_id,forum_comments.added_date,group_id,'made a comment on the topic',3,forum_comments.forum_topic_id,title FROM forum_comments
INNER JOIN forum_topics ON forum_comments.forum_topic_id = forum_topics.forum_topic_id
WHERE forum_topics.forum_categories_id = 16 AND forum_topics.group_id > 0 AND forum_topics.status = 1 AND forum_comments.status = 1
)
This gets all the activity from a set of groups. My question is at the end I want to make sure that the group is active.
So at the end want to do something like WHERE (SELECT COUNT(1) FROM groups g WHERE g.group_id = group_id AND status = 1) = 1
Is there any way of doing that?
i'd suggest to store it to a view or temporary table and query the view then. i know you will have two calls then, but it's actually faster in mysql that way.