简体   繁体   中英

Distinct With Conditions in MYSQL

Having a table such as this (only an example):

Program category status 
A         cat1    active
B         cat2    inactive
C         cat3    active
D         cat2    active

I would like to get distinct categories but only if the status is active if I use

SELECT DISTINCT category FROM table WHERE status='active'

it will not include cat2 as a distict value even though it appeared once as active. I would like all distinct categories returned even if it appears just once as active.

I could use a php loop to do this but I would rather not return every category being this is a large table.

Thank you in advance for any help. First time posting but StackOverFlow has helped me many many times.


The actual table structure is

CREATE TABLE cats (
    id int(11) NOT NULL AUTO_INCREMENT,
    programname varchar(100) NOT NULL DEFAULT '',
    sub0 varchar(300) DEFAULT NULL,
    sub1 varchar(300) DEFAULT NULL,
    sub2 varchar(300) DEFAULT NULL,
    sub3 varchar(300) DEFAULT NULL,
    sub4 varchar(300) DEFAULT NULL,
    category varchar(300) DEFAULT NULL,
    status varchar(1) DEFAULT NULL,
    PRIMARYKEY (id)
) ENGINE= InnoDBDEFAULT CHARSET= latin1;

It seems to work like this:

SQL DEMO

select distinct category
from demo
where status = 'active';

| CATEGORY |
------------
|     cat1 |
|     cat3 |
|     cat2 |

Unless you really have some other conditions to apply here, which you may need to let us know :)

I still cannot reproduce the problem.
The output of the following example is

Books>Art>Modern
Books>Art>Concrete
Books>Art>Classicism
Books>Art>Naive

There are two records having category=Books>Art>Classicism . The first has status=0 and is excluded by the WHERE clause while the second has status=1 .
You can check that there are no typos in my example data by setting the status of the first record from 0 to 1 and you will see that Books>Art>Classicism is still only displayed once but right after Books>Art>Modern as the second record.

<?php
$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
setup($pdo);

$stmt = $pdo->query("SELECT DISTINCT category FROM soFoo WHERE status='1'", PDO::FETCH_ASSOC);
foreach( $stmt as $row ) {
    echo join(', ', $row), "\n";
}


function setup($pdo) {
    $pdo->exec("
        CREATE TEMPORARY TABLE soFoo (
            id int(11) NOT NULL AUTO_INCREMENT,
            programname varchar(100) NOT NULL DEFAULT '',
            sub0 varchar(300) DEFAULT NULL,
            sub1 varchar(300) DEFAULT NULL,
            sub2 varchar(300) DEFAULT NULL,
            sub3 varchar(300) DEFAULT NULL,
            sub4 varchar(300) DEFAULT NULL,
            category varchar(300) DEFAULT NULL,
            status varchar(8) DEFAULT NULL,
            PRIMARY KEY (id)
        ) ENGINE= InnoDB DEFAULT CHARSET= latin1;
    ");
    $stmt = $pdo->prepare('
        INSERT INTO
            soFoo
            (
                programname, sub0, sub1, sub2,
                sub3, sub4,
                category, status
            )
        VALUES
            (
                :programname, :sub0, :sub1, :sub2,
                :sub3, :sub4,
                :category, :status
            )
    ');
    $data = array();
    $data[] = array(
        'programname'=>'BOOK Store',
        'sub0'=>'Books', 'sub1'=>'Art Books','sub2'=>'Modern Art',
        'sub3'=>null, 'sub4'=>null,
        'category'=>'Books>Art>Modern', 'status'=>'1'
    );
    // this one has category='Books>Art>Classicism' and status=0
    // the second but last element is a copy of this one with status=1
    $data[] = array(
        'programname'=>'BOOK Store',
        'sub0'=>'Books', 'sub1'=>'Art Books','sub2'=>'Modern Art',
        'sub3'=>null, 'sub4'=>null,
        'category'=>'Books>Art>Classicism', 'status'=>'0'
    );
    $data[] = array(
        'programname'=>'BOOK Store',
        'sub0'=>'Books', 'sub1'=>'Art Books','sub2'=>'Modern Art',
        'sub3'=>null, 'sub4'=>null,
        'category'=>'Books>Art>Modern', 'status'=>'1'
    );
    $data[] = array(
        'programname'=>'BOOK Store',
        'sub0'=>'Books', 'sub1'=>'Art Books','sub2'=>'Modern Art',
        'sub3'=>null, 'sub4'=>null,
        'category'=>'Books>Art>Concrete', 'status'=>'1'
    );
    $data[] = array(
        'programname'=>'BOOK Store',
        'sub0'=>'Books', 'sub1'=>'Art Books','sub2'=>'Modern Art',
        'sub3'=>null, 'sub4'=>null,
        'category'=>'Books>Art>Classicism', 'status'=>'1'
    );
    $data[] = array(
        'programname'=>'BOOK Store',
        'sub0'=>'Books', 'sub1'=>'Art Books','sub2'=>'Modern Art',
        'sub3'=>null, 'sub4'=>null,
        'category'=>'Books>Art>Naive', 'status'=>'1'
    );

    foreach( $data as $rec ) {
        $stmt->execute($rec);
    }
}

Try this:

SELECT DISTINCT category 
FROM 
(SELECT category FROM table WHERE status='active') as temp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM