...

View Full Version : Resolved get altenate data if there is no demanded data



BubikolRamios
02-16-2009, 07:40 PM
data, for 'en' there is always data.


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 ?

Old Pedant
02-17-2009, 12:55 AM
Hmmm...a fun one!

First thing I would try off the top of my head (UNTESTED!) would be


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.

BubikolRamios
02-17-2009, 05:37 AM
excelent !

small correction:


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.

Old Pedant
02-17-2009, 07:11 AM
Whoops...nice catch on the need for the WHERE clause there.

Well, I did say it was untested. Sorry!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum