Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,963
    Thanks
    120
    Thanked 76 Times in 76 Posts

    get altenate data if there is no demanded data

    data, for 'en' there is always data.
    Code:
    1   en   dog
    1   sl    pes
    2   en   peacock
    ....
    1.get all for 'it' from that data, and if it does not exist get 'en' -->
    1 dog
    2 peacock

    2.get all for 'sl' from that data, and if it does not exist get 'en'-->
    1 pes
    2 peacock

    query that would do all that provided one parameter 'en','it',or 'sl', or any other language ?
    Last edited by BubikolRamios; 02-17-2009 at 06:01 AM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,458
    Thanks
    76
    Thanked 4,373 Times in 4,338 Posts
    Hmmm...a fun one!

    First thing I would try off the top of my head (UNTESTED!) would be
    Code:
    SELECT W.id, IF( SL.id IS NULL, W.word, SL.word ) AS TheWord
    FROM words AS W 
    LEFT JOIN ( SELECT id, language, word FROM words WHERE language = 'sl' ) AS SL
    ON W.id = SL.id
    WHERE W.id IN ( 1, 2 )
    Again, untested (I don't have MYSQL on this machine...don't ask why not), but feels right.

  • Users who have thanked Old Pedant for this post:

    BubikolRamios (02-17-2009)

  • #3
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,963
    Thanks
    120
    Thanked 76 Times in 76 Posts
    excelent !

    small correction:
    Code:
    SELECT W.id, IF( SL.id IS NULL, W.word, SL.word ) AS TheWord
    FROM words AS W 
    LEFT JOIN ( SELECT id, language, word FROM words WHERE language = 'sl' ) AS SL
    ON W.id = SL.id
    WHERE W.language = 'en'

    PS

    Lately I 'm writing qustions that nobody knows the answer, or ..... are fun (-:

    Glad that there are people outthere finding my questions interesting.
    Last edited by BubikolRamios; 02-17-2009 at 05:39 AM.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,458
    Thanks
    76
    Thanked 4,373 Times in 4,338 Posts
    Whoops...nice catch on the need for the WHERE clause there.

    Well, I did say it was untested. Sorry!


  •  

    Posting Permissions

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