How can I use Zend_Db_Select to directly select from a subquery (view)?
I have 5 tables with the same structure, I want to get all the rows from them, combine them and remove duplicates. I use UNION, which automatically removes duplicates. The problem is that I add a static column for each table before, so there is one column that differs => duplication.
Here is my request:
SELECT `news_main`.*, 'main' as `category` FROM `news_main` UNION SELECT `news_politics`.*, 'politics' as `category` FROM `news_politics` UNION SELECT `news_society`.*, 'society' as `category` FROM `news_society` UNION SELECT `news_world`.*, 'world' as `category` FROM `news_world` UNION SELECT `news_business`.*, 'business' as `category` FROM `news_business` ORDER BY `date` DESC LIMIT 8
See how to add static values ββto a new category column? Now everything else is the same (there are duplicate lines), but since they are from different categories, UNION cannot delete them.
So I thought that I could SELECT remove all rows from this subquery and group them to remove duplicates, for example:
SELECT * FROM ( SELECT `news_main`.*, 'main' as `category` FROM `news_main` UNION SELECT `news_politics`.*, 'politics' as `category` FROM `news_politics` UNION SELECT `news_society`.*, 'society' as `category` FROM `news_society` UNION SELECT `news_world`.*, 'world' as `category` FROM `news_world` UNION SELECT `news_business`.*, 'business' as `category` FROM `news_business` ORDER BY `date` DESC LIMIT 8 ) as subtable GROUP BY `source` ORDER BY `date` DESC
I ran this in MySQL and it works great. The only problem is ...
How to do this with Zend_Db_Select fancy functions?
Thanks in advance!