SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Resistance is Futile webgodjj's Avatar
    Join Date
    Nov 2002
    Location
    Madison, WI USA
    Posts
    448
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    using if statement in a select and a possible subselect?

    Hi,

    I'm trying to create a custom language script in php and mysql. Basically how I have it now, Is I do two queries.... one I try selecting the language in the database. To see if any record for that page exists in the second language. If true the second sql statment will pull on that language id, if not, then it will pull on the default language id.

    So I'm using this table:

    PAGES
    id | page_id | language_id | page_meta_title | page_title | page_copy

    Can I do a single query, ie, select distinct page_id value, where page_id = "current page id" & language_id = "current selected language"

    if no value

    select * from PAGES where page_id = "current page id" and language_id = 1

    else

    select * from PAGES where page_id = "current page id" and language_id = "current selected language"
    Atomic Coffee Media
    http://www.atomiccoffee.com
    JJ Pagac

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    something like: (untested)

    Where: German is desired and English is the default (known to exist) - replace languages_id with proper surrogate key

    Code SQL:
    SELECT
         IF(t.pages_id IS NULL,d.page_meta_title,t.page_meta_title) page_meta_title
         ,IF(t.pages_id IS NULL,d.page_title,t.page_title) page_title
         ,IF(t.pages_id IS NULL,d.page_copy,t.page_copy) page_copy
      FROM
         pages d
      LEFT OUTER
      JOIN
         pages t
        ON
         d.pages_id = t.pages_id
       AND
         t.languges_id = 'german'
     WHERE
         d.pages_id = 89
       AND
         d.languages_id = 'english'


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
  •