SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    May 2006
    Location
    Ljubljana
    Posts
    241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mysql joining 2 tables (default values)

    Hello..

    I came to situation where I have to join 2 tables on some other table:

    some_table:
    id
    domain

    table domains:
    id
    domaintype_id
    domain

    table domaintypes:
    id
    type

    This is how I join:

    domains ON some_table.domain = domains.domain
    domaintypes ON domaintype.id = domains.domaintype_id

    Now it happens that sometimes there is no domain for some_table row (so the domaintype will be NULL) and I want to be able to set this column default value 0 (when that happens)..

    How is it possible to do that?

    Many thanks in advance!

  2. #2
    SitePoint Guru
    Join Date
    Jan 2004
    Location
    Uppsala, sverige
    Posts
    696
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Code:
    select coalesce(domain_id,0), ...
      from some_table
      left join domains on some_table.domain = domains.domain
      left join domaintypes on domaintype.id = domains.domaintype_id


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
  •