从SQL结果中删除重复项并显示其他列

时间:2018-06-15 09:53:00

标签: mysql sql

我正在尝试从我的数据库中获取信息,但在保留其他列数据的同时删除重复值。我尝试过使用distinct,因为它是我能找到的唯一真实信息。我尝试了以下,这有效删除重复的名称,所以我有“Name Name2 Name3”删除重复的名称2:

SELECT DISTINCT Column 1 FROM table WHERE column3 LIKE'Place1'

我现在坚持尝试让它显示第3列和第2列数据,下面是表格当前的样子:

 | Column 1 | Column 2 | Column 3 |
 | Name     | 71       | Item1    |
 | Name2    | 78       | Item1    |
 | Name2    | 77       | Item1    |
 | Name3    | 75       | Item1    |
 | Name     | 75       | Item2    |

以下是我想要得到的结果:

 | Column 1 | Column 2 | Column 3 |
 | Name     | 71       | Item1    |
 | Name2    | 77       | Item1    |
 | Name3    | 75       | Item1    |

任何正确方向的帮助或观点都会令人惊叹!

由于

3 个答案:

答案 0 :(得分:1)

您必须选择第2列中显示的数据;如果你想随机选择,你可以这样做:

;(function () {
    window.addEventListener('load', initStickyBlock);
    window.addEventListener('resize', myEfficientFn);
} ());

function initStickyBlock() {
    var body = document.getElementsByTagName('body')[0];
    var header = document.getElementById('header');
    var burgerBtn = document.getElementById('burger');
    var blocks = document.querySelectorAll('.js-fixed-block');
    var containerFilter = document.querySelector('.js-fixed-container');
    var classFixed = 'fixed-position';
    var classAfterFixed = 'after-fixed';
    var pageY = null;

    var options = [].slice.call(blocks).map(function(item, i) {
        return {
            item: item,
            recoup: stringToBoll(item.dataset.recoup),
            topOffset: !item.dataset.start ? header.offsetHeight : 0,
            topStart: item.dataset.start ? parseFloat(item.dataset.start) : getOffsetTop(item) - header.offsetHeight
        }
    });

    function stringToBoll(item) {
        return (/^true$/i).test(item);
    };

    function getOffsetTop(item) {
        return item.getBoundingClientRect().top + pageYOffset
    }

    function setTop(isActive, options) {
        options.item.style.top = isActive ? options.topOffset + 'px' : ''
    }

    function recoupHeight(isActive, options) {
        options.item.closest('.js-container').style.paddingTop = isActive ? options.item.offsetHeight + 'px' : 0
    }

    function toogleFixedClass(isActive, options) {
        options["item"].classList[isActive ? 'add' : 'remove'](classFixed);
    }

    function stopAfterFixed(isAfterFixed, options) {
        options["item"].classList[isAfterFixed ? 'add' : 'remove'](classAfterFixed);
    }

    function onScroll() {
        var scrollTop = window.pageYOffset || document.scrollTop || this.scrollY;

        for (var i = 0; i < options.length; i++) {
            var isAfterFixed = containerFilter ? scrollTop > getOffsetTop(containerFilter) + containerFilter.offsetHeight - options[i].item.offsetHeight - options[i].topOffset : null;
            var isActive = scrollTop > options[i].topStart && !isAfterFixed;

            if (options[i].recoup) {
                recoupHeight(isActive, options[i]);
            }

            if (options[i].topOffset) {
                setTop(isActive, options[i])
            }

            toogleFixedClass(isActive, options[i]);
            stopAfterFixed(isAfterFixed, options[i]);
        }
    }

    function onBurgerChange(e) {
        var isOpen = e.currentTarget.checked;

        if (isOpen) {
            pageY = window.pageYOffset || document.scrollTop || this.scrollY;
            body.style.top = -pageY + 'px';
        } else {
            body.style.top = '';
            setTimeout(function() {
                window.scrollTo(0, pageY);
            }, 0)
        }

        body.classList[isOpen ? 'add' : 'remove']('nav-active');
    }

    onScroll();
    window.addEventListener('scroll', onScroll);
    burgerBtn.addEventListener('change', onBurgerChange);
}

var myEfficientFn = debounce(function() {
    initStickyBlock();
}, 250);

function debounce(func, wait, immediate) {
    var timeout;
    return function() {
        var context = this, args = arguments;
        var later = function() {
            timeout = null;
            if (!immediate) func.apply(context, args);
        };
        var callNow = immediate && !timeout;
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
        if (callNow) func.apply(context, args);
    };
};

;(function() {
    if (!Element.prototype.closest) {
        Element.prototype.closest = function(css) {
            var node = this;

            while (node) {
                if (node.matches(css)) return node;
                else node = node.parentElement;
            }
            return null;
        };
    }
})();

;(function() {
    if (!Element.prototype.matches) {
        Element.prototype.matches = Element.prototype.matchesSelector ||
        Element.prototype.webkitMatchesSelector ||
        Element.prototype.mozMatchesSelector ||
        Element.prototype.msMatchesSelector;
    }
})();

编辑:如果你想在Column2上使用聚合函数的随机值,你可以这样做:

SELECT Column1, Column2, Column3  FROM table WHERE column3 LIKE 'Item1' GROUP BY Column1

答案 1 :(得分:0)

从我想要的输出,我认为,以下查询将完成工作

SELECT c1, c2, c3  
FROM t
WHERE c3 LIKE 'i1'
  and c2 in (select min(c2) from t group by c1)

SQL Fiddle

答案 2 :(得分:0)

我建议:

select t.column1, min(t.column2) as column2, min(t.column3) as column3
from t
where column3 = 'Item1'
group by t.column1;

这会为column1中的每个值返回一行,column2的最小值为column3 'Item1'