User:DWalden (WMF)/(Un)delete associated talk page/sql page

SET @pageid=<page id>;
SET @pagetitle="<page title>"; -- Replace any spaces in the page title with underscores.
SELECT SUM(CASE WHEN in_archive = "False" THEN 1 ELSE 0 END) AS "Rows in revision", SUM(CASE WHEN in_archive = "True" THEN 1 ELSE 0 END) AS "Rows in archive", IF(ISNULL(page_id), "Yes", "No") AS "Page deleted?", log_count, pr_count, rd_from AS Redirect, rc_count, revcomment_rev, revactor_rev, pl_count, cl_count, il_count, el_count, iwl_count, ll_count, tl_count FROM
(
SELECT rev_page AS our_page_id, "False" AS in_archive, rev_id AS revid
FROM revision
WHERE rev_page=@pageid
UNION ALL
SELECT ar_page_id AS our_page_id, "True" AS in_archive, ar_rev_id AS revid
FROM archive
WHERE ar_page_id=@pageid
) AS foo
LEFT JOIN page ON our_page_id=page_id
LEFT JOIN (SELECT COUNT(*) AS log_count FROM logging WHERE (log_page=@pageid OR log_title=@pagetitle)) AS log ON TRUE
LEFT JOIN (SELECT COUNT(*) AS pr_count FROM page_restrictions WHERE pr_page=@pageid) AS pr ON TRUE
LEFT JOIN redirect ON our_page_id=rd_from
LEFT JOIN (SELECT COUNT(*) AS rc_count FROM recentchanges WHERE rc_cur_id=@pageid OR rc_title=@pagetitle) AS rc ON TRUE
LEFT JOIN revision_comment_temp ON revid=revcomment_rev
LEFT JOIN revision_actor_temp ON revid=revactor_rev
LEFT JOIN (SELECT COUNT(*) AS pl_count FROM pagelinks WHERE pl_from=@pageid) AS pl ON TRUE
LEFT JOIN (SELECT COUNT(*) AS cl_count FROM categorylinks WHERE cl_from=@pageid) AS cl ON TRUE
LEFT JOIN (SELECT COUNT(*) AS il_count FROM imagelinks WHERE il_from=@pageid) AS il ON TRUE
LEFT JOIN (SELECT COUNT(*) AS el_count FROM externallinks WHERE el_from=@pageid) AS el ON TRUE
LEFT JOIN (SELECT COUNT(*) AS iwl_count FROM iwlinks WHERE iwl_from=@pageid) AS iwl ON TRUE
LEFT JOIN (SELECT COUNT(*) AS ll_count FROM langlinks WHERE ll_from=@pageid) AS ll ON TRUE
LEFT JOIN (SELECT COUNT(*) AS tl_count FROM templatelinks WHERE tl_from=@pageid) AS tl ON TRUE;