テーブルのリスト (つまり、productsA、productsB、productsN、...) があります。これらのテーブルの各製品には、コメント (comments テーブルに格納されている) がある場合があります。 (パフォーマンスと速度の面で) 採用されるソリューションは?
ユニオンを使用:
http://www.sqlfiddle.com/#!3/bc382/1
select TOP 10 comment_product, product_name, comment_date FROM (
select comment_product, product_name, comment_date from comments inner join productsA on product_id = id_product WHERE product_type = 'A'
UNION
select comment_product, product_name, comment_date from comments inner join productsB on product_id = id_product WHERE product_type = 'B'
UNION
select comment_product, product_name, comment_date from comments inner join productsC on product_id = id_product WHERE product_type = 'C'
) as temp ORDER BY comment_date DESC
ケースの使用:
http://www.sqlfiddle.com/#!3/bc382/2
select TOP 10 comment_product, comment_date,
CASE product_type
when 'A' then (select product_name from productsA as sub where sub.id_product = com.product_id)
when 'B' then (select product_name from productsB as sub where sub.id_product = com.product_id)
when 'C' then (select product_name from productsC as sub where sub.id_product = com.product_id)
END
FROM comments as com
ORDER BY comment_date DESC