doubingling4706 2014-08-13 17:40
浏览 270

mysql DISTINCT关键字不适用于多列连接表

I have used mysql DISTNCT keyword for one of my mySQL query for avoid duplication of data displaying on search box.

But when I add multiple column names on SELECT it doesn't work.

Please advice how to proceed.

$query = "

    SELECT * FROM (
        SELECT DISTINCT b.title,b.id, b.metakey
            FROM categories b
            WHERE  b.title like '%".$searchc."%' AND b.parent_id BETWEEN 84 AND 107 AND  b.level=3 ORDER BY LOCATE('".$searchc."', REPLACE(b.title, ' ', '')), b.title
    ) CLASS_CAT
        UNION ALL
            SELECT * FROM (
                SELECT DISTINCT a.title,  a.id, a.title as metakey
                    FROM content a join
                      categories b
                      on a.categories_id = b.id
                WHERE REPLACE(a.title, ' ', '') like '%".$searchc."%' 
                AND b.parent_id BETWEEN 84 AND 107 AND b.level=3 
             ) CLASS_ITEM
";
  • 写回答

1条回答 默认 最新

  • dongyingjiu0669 2014-08-13 17:49
    关注

    SELECT DISTINCT will remove duplicates from the one SELECT statement. UNION ALL directs the system to not look for duplicates between the two sets you are combining (each SELECT).

    Use UNION (without ALL) instead. Note that removing the check for duplicates is faster, so if you know a set you're querying is unique, skip the dup check.

    Also note that by row duplicates I'm referring to every column in every row. If any column makes a row unique it will appear in the result set. If you only want some columns to be unique you'll need to GROUP BY and aggregate the other columns (e.g. GROUP_CONCAT) or use additional queries to get other related data.

    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?