Home  >  Article  >  Backend Development  >  mysql UNION 有重复的数据,如何解决

mysql UNION 有重复的数据,如何解决

WBOY
WBOYOriginal
2016-06-13 13:26:151017browse

mysql UNION 有重复的数据,怎么解决?
SELECT * , 1 AS xh
FROM text
WHERE (
id AND `title` LIKE '%德国%'
AND `title` LIKE '%丹麦%'
)
UNION SELECT * , 2 AS xh
FROM text
WHERE (
id AND `title` LIKE '%德国%'
OR `title` LIKE '%丹麦%'
)
ORDER BY xh
LIMIT 6

------解决方案--------------------
改成'x''X'.....这么诡异的注意你也能想出来....嗯, 至少要加个注释,要不将来被N个读代码的骂不要怪我.....

关于括号: 
id 相当于 ( id
这个和
 id 是不同的, 应该明白了吧, 

还不明白加条 有"丹麦"并且 id>312的记录 并且能让它排进前6的话就明白了
------解决方案--------------------
不至于搞得那么复杂吧?

SQL code
SELECT * , 1 AS xh
FROM text
WHERE (
id 
                 
              
              
        
            
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn