这是我目前的查询:
SELECT
topic.title,
topic.content_id,
topic.thumbnail_icon,
text.description
FROM
mps_contents AS topic,
mps_contents AS text
WHERE
topic.featured = '1' AND
topic.active = '1' AND
topic.page_id = (SELECT page_id
FROM mps_pages
WHERE page_short_name = 'services'
OR page_short_name = 'questions_faqs') AND
text.section_id = (SELECT section_id
FROM mps_sections
WHERE section_short_name = 'page_text') AND
text.page_id = topic.content_id AND
topic.resource_type = 'subpage' AND
text.resource_type = 'text'
ORDER BY RAND() LIMIT 2
修改
这是MySQL
我真正想做的是从services
中选择1个特色项目和从questions
中选择1个特色项目。不过,此查询会从services
或questions
中选择2个特色商品!
我知道一个选项是在UNION
的两个查询中生成LIMIT 1
并执行UNION
,但这意味着我必须复制90%的查询。我可以这样做,而不必诉诸UNION
s?
答案 0 :(得分:0)
好吧,我相信除非你重新设计/规范化表格,否则你总会在某种程度上重复查询。
如果你想避免结合,你可以重写
topic.page_id = (SELECT page_id
FROM mps_pages
WHERE page_short_name = 'services'
OR page_short_name = 'questions_faqs')
到
topic.page_id = (SELECT page_id
FROM mps_pages
WHERE page_short_name = 'services' ORDER BY RAND() LIMIT 1)
OR
topic.page_id = (SELECT page_id
FROM mps_pages
WHERE page_short_name = 'questions_faqs' ORDER BY RAND() LIMIT 1)
虽然rand()运行两次
,但我认为不会有太多的性能提升