MySql:如何选择所有值相同的行?

时间:2018-06-05 07:14:30

标签: mysql

我有一张这样的表:

name |id | state
name1 12   4
name1 12   4
name2 33   3
name2 33   4
...

我想从表中选择 4的每个名称和ID,这意味着 name1 是正确的,因为它只有两个状态为4且没有记录的记录更多。同时 name2 是错误的,因为它有状态4 记录状态为3的记录。

5 个答案:

答案 0 :(得分:1)

select name, id from mytable where id not in
(select distinct id from mytable where state <> 4)

答案 1 :(得分:1)

您可以使用聚合,如下所示:

SELECT name, id
FROM your_table
GROUP BY name, id
HAVING SUM(state<>4)=0;

查看Demo on SQL Fiddle

答案 2 :(得分:0)

您可能需要2个子查询。

  1. 按名称分组选择状态为4
  2. 按名称分组选择
  3. 如果计数相同则比较计数,然后选择它

    示例:从表中选择名称,计数(名称),其中state = 4作为T1           从表中选择名称,计数(名称)为T2            从T1和T2中选择T1.name,其中T2.count = T1.count

答案 3 :(得分:0)

你可以使用不存在这样的:

 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <project basedir="." default="main" name="MerckDemo">
        <property environment="env" />
        <property name="debuglevel" value="source,lines,vars" />
        <property name="target" value="1.8" />
        <property name="source" value="1.8" />
        <property name="src.dir" location="src" />
        <property name="build.dir" location="bin" />
        <property name="dist.dir" location="dist" />
        <property name="resources" location="resources" />
        <property name="jardest" location="jardest" />
        <property name="docs.dir" location="docs" />
        <property name="lib" location="lib" />


        <path id="build.classpath">
            <fileset dir="${lib}">
                <include name="tessract-wrapper.jar" />
                <include name="testng-6.11.jar" />
                <include name="iwats-scripting-library-v1.9.jar" />
                <include name="json-20160810.jar" />
                <include name="jcommander-1.27.jar" />
            </fileset>
        </path>

        <pathconvert property="classpathProp" refid="build.classpath" />
        <echo>Classpath is ${classpathProp}</echo>

        <target name="clean">
            <delete dir="${build.dir}" />
            <delete dir="${dist.dir}" />
            <delete dir="${docs.dir}" />
        </target>

        <target name="init">
            <mkdir dir="bin" />
            <mkdir dir="dist" />
            <mkdir dir="docs" />

        </target>
        <target name="compile" depends="clean,init">
            <javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="build.classpath" includeantruntime="false">
            </javac>
        </target>

        <target name="docs">
            <javadoc sourcepath="${src.dir}" destdir="${docs.dir}">
                <fileset dir="${src.dir}">
                    <include name="**" />
                </fileset>
            </javadoc>
        </target>
        <target name="setproperties">
            <copy file="testproperties.templete" tofile="test.properties" overwrite="true">
                <filterset begintoken="@" endtoken="@">
                    <filter token="application_type" value="${application_type}" />
                    <filter token="remote_host" value="${remote_host}" />
                    <filter token="remote_port" value="${remote_port}" />
                    <filter token="testng_group" value="${testng_group}" />
                    <filter token="testng_package" value="${testng_package}" />
                </filterset>
            </copy>

        </target>

        <target depends="compile,setproperties" name="main">
            <java classname="merck.trigger.Trigger" classpath="${build.dir}" classpathref="build.classpath" fork="true">

            </java>
        </target>
        <target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects" />
    </project>

答案 4 :(得分:0)

在更一般的情况下,您可以使用count distinct(不存在或使用连接):

(function () {
    appModule.controller('augustinum.views.kostenstelle.index', [
        '$scope', '$uibModal', 'abp.services.app.kostenstelle',
        function ($scope, kostenstelleService) {
            var vm = this;

            vm.kostenstellen = [];

            vm.getKostenstelle = function () {
                kostenstelleService.getKostenstelle().then(function (result) {
                    vm.kostenstellen = result.items;
                    console.log("Step1" + vm.kostenstellen.length);
                });
            };

            vm.getKostenstelle();   
            console.log("Step2" + vm.kostenstellen.length);
        }
    ]);
})();