Mysql Union重复结果

时间:2010-01-22 11:43:32

标签: mysql

我有以下选择,但它重复第二次内部选择的结果3次。

任何人都可以告诉我为什么会这样。

当我得到结果时,我怎么知道结果来自哪个表。 Home_content或facilities_table。

SELECT * FROM (SELECT hm_id, hm_name, hm_summary, 
MATCH (hm_name, hm_summary) AGAINST  ('test') AS score FROM home_content 
WHERE MATCH (hm_name, hm_summary) AGAINST ('test') UNION SELECT  fac_id,fac_name,          
fac_summary, MATCH (fac_title, fac_summary) AGAINST ('test') AS score FROM 
facilities_table WHERE MATCH (fac_title, fac_summary) AGAINST ('test')) a 
ORDER BY SCORE DESC

提前致谢

1 个答案:

答案 0 :(得分:0)

除非:

,否则无法查看您从此查询中获取重复项的原因
  1. 源表中有重复项
  2. 相同的数据出现在home_content和facilties_table中(也许一个是另一个的视图?)
  3. 关于决定联合内容来自哪个表的问题的第二部分很容易通过在联合的每个查询中添加一个常量列来解决这个问题:

    SELECT * FROM (SELECT 1,hm_id, hm_name, hm_summary, 
    MATCH (hm_name, hm_summary) AGAINST  ('test') AS score FROM home_content 
    WHERE MATCH (hm_name, hm_summary) AGAINST ('test') UNION SELECT  2,fac_id,fac_name,          
    fac_summary, MATCH (fac_title, fac_summary) AGAINST ('test') AS score FROM 
    facilities_table WHERE MATCH (fac_title, fac_summary) AGAINST ('test')) a 
    ORDER BY SCORE DESC
    

    在这种情况下,home_content表的初始列应为1,而facility_table的初始列应为2。显然字符串常量可以用于类似的风格,如果这更适合你。