LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-25-2004, 07:11 AM   #1
synna
Member
 
Registered: Jan 2004
Posts: 40

Rep: Reputation: 15
MySQL question


Hi,

I'm writting a multilingual news system and I have this two table (here I will simplify it)
Code:
TABLE news( 
   newsid mediumint(8) NOT NULL PRIMARY KEY
   #other info
);

TABLE newscontent(
    newsid mediumint
    languageid varchar(5)
    UNIQUE(newsid,languageid)
     #inof like title, content....
);
Now what I want to do is is select all news with the current language, if a news isn't translated then select the défault (english).

I mean this won't work :
Code:
SELECT news.*, newscontent.* FROM news, newscontent WHERE news.newsid=newscontent.newsid
SELECT news.*, newscontent.* FROM news, newscontent WHERE news.newsid=newscontent.newsid AND newscontent.languageid=$currentlanguage
The first one select all availlable language for each news.
The seconde doesn't select a news if no translation is availlable (here we must also show english).

I want to have a single row for each news.

Any idea about an sql command that could do this ?

Maybe it's not possible in MySQL and I have to code the language selection separately ?

I'm quite sure that MySQL can do it much faster than a coded thing (currently PHP).


Thanks.

Last edited by synna; 02-25-2004 at 07:20 AM.
 
Old 02-25-2004, 09:40 AM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Code:
SELECT news.*, newscontent.*
FROM news, newscontent AS nc1
WHERE news.newsid=newscontent.newsid AND    
    (newscontent.languageid=$currentlanguage OR
        (newscontent.languageid=$english AND 
            NOT EXISTS 
                (SELECT * 
                FROM newscontent
                WHERE newscontest.newsid=nc1.newsid AND 
                newscontent.language=$currentlanguage)
       )
)
It should work (not 100% sure) but you mey need new MySQL to do it (to support my EXISTS). Plus it's probably not the fastest way to do it.
 
Old 02-25-2004, 11:36 AM   #3
synna
Member
 
Registered: Jan 2004
Posts: 40

Original Poster
Rep: Reputation: 15
I'm quite sure it can work but it doesn't run on the server (generate an error) as EXISTS is only from version 4.0 and the server is 3.23.52 (and I can't change it).

So any idea more... ?
 
Old 02-25-2004, 01:37 PM   #4
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
Code:
select n2.* 
from newscontent as n1 join newscontent as n2 
 on n1.newsid=n2.newsid and n1.languageid!='eng' and n2.languageid=$currentlanguage
union 
select * from newscontent where languageid=$currentlanguage
mysql 3.x doesnt have union so you can either run two seperate queries or follow the instructions here

i think it works but theres bound to be a much faster/better way.

Last edited by kev82; 02-25-2004 at 01:47 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
mysql error Can't connect to local MySQL server through socket '/var/lib/mysql/mysql. Dannux Linux - Software 3 03-24-2006 08:44 AM
MySQL question... kith Linux - General 3 03-06-2004 06:40 PM
MySQL Question NoBrains Linux - Software 2 03-01-2004 10:10 PM
MySQL question TraCe66 Linux - Newbie 11 07-15-2003 02:35 PM
MySQL question ascii2k Linux - General 1 03-27-2002 04:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration