SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Select distinct column1, unless column2 is not null?

    Please help queryng my db, selecting a distinct colunm unless another column is not null, if it's not null, then column can repeat?

    here's my table:

    Code:
    mysql> describe example;
    +--------+-------------+------+-----+---------+----------------+
    | Field  | Type        | Null | Key | Default | Extra          |
    +--------+-------------+------+-----+---------+----------------+
    | id     | int(10)     |      | PRI | NULL    | auto_increment |
    | name   | varchar(50) | YES  |     | NULL    |                |
    | field1 | varchar(20) | YES  |     | NULL    |                |
    | field2 | varchar(20) | YES  |     | NULL    |                |
    +--------+-------------+------+-----+---------+----------------+
    4 rows in set (0.00 sec)
    selecting everything from the table, gives me this

    Code:
    mysql> select * from example;
    +----+------+--------+--------+
    | id | name | field1 | field2 |
    +----+------+--------+--------+
    |  1 | mike | aaaa   | NULL   |
    |  2 | john | aaaa   | NULL   |
    |  3 | john | bbbb   | NULL   |
    |  4 | john | cccc   | NULL   |
    |  5 | mike | NULL   | ffff   |
    |  6 | mike | NULL   | gggg   |
    |  7 | john | dddd   | NULL   |
    +----+------+--------+--------+
    7 rows in set (0.00 sec)
    What I want is to select distinct name, unless field 2 is not null, if it's not null, then repeating the name for that row is ok

    if I select distinct name I get this:
    Code:
    mysql> select distinct name from example;
    +------+
    | name |
    +------+
    | mike |
    | john |
    +------+
    2 rows in set (0.00 sec)
    But I want a result set to return something like this:

    Code:
    +------+
    | name |
    +------+
    | mike |
    | john |
    | mike |
    | mike |
    +------+
    That is, distinct names, but since mike has two rows where the field2 is not null, it gets returned

    I thought maybe UNIONs would do but I must make this work on Mysql 3.xx

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,347
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Code:
    select name 
      from example
    group
        by name
         , field2  
    ;
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937
    Code:
    select name 
      from example
    group
        by name
         , field2  
    ;
    Wow that's great, exactly what I need! Guess I need to go back to SQL basics


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •