I am having following select query
(select article.articleId ,'MUSIC' as type ,createddate
FROM MUSICarticle
where article.status=2
and article.createdBy=101
order by createdDate ASC limit 1)
UNION
(select article.articleId, 'SPORTS' as type ,createddate
FROM SPORTSarticle
where article.status=2
and article.createdBy=101
order by createdDate ASC limit 1)
UNION
(select article.articleId ,'DANCE' as type ,createddate
FROM DANCE article
where article.status=2
and article.createdBy=101
order by createdDate ASC limit 1)
UNION
(select article.wikiTopicId as articleId ,'SINGING' as type ,createddate
FROM SINGING article
where article.status=2
and article.createdBy=101
order by createdDate ASC limit 1)
order by 3 ASC limit 1
and having output
articleid | type | createddate
----------+---------|-------------------------
1 | MUSIC | 2017-04-28 12:53:55.649
Now have to update MUSIC table where id =1
.
Best Answer
You could use a stored procedure or anonymous PL block to build dynamic SQL. See below for an example.