开发者

Custom Order By + Group By

So I have a manual in this table:

id  lang    header_name        text         
1   uk       Youth Development  It's very important
2   dk       tst                hejsa   
3   uk       tst                hello sir

And I want to make a query that fetches all manual entries for a given language (danish in this case). If for some reason not all 100% of the original manual entries (the UK ones), has been t开发者_JAVA技巧ranslated I want to get the english entry instead. Is that even possible in table formats such as this?

I guess it would be something with a "group by header_name" of some sorts, but not sure.


Try this, i dont have an SQL and hence this is not tested The tables t1, t2, t3 refer to the same table use an alias to distinguish them;

select * from t3  
where t3.lang IN ('DK','UK')  
and t3.ID NOT IN  
(select t1.id  
FROM t1,t2  
where t1.header_name = t2.header_name  
AND t2.lang = 'DK'  
AND t1.lang = 'UK'  
)  

Essentially first you need to find the ID that have translation, and then exclude them.


This might do the trick but it is not optimized:

SELECT *
FROM the_table
WHERE lang = 'dk'

UNION

SELECT *
FROM the_table
WHERE lang <> 'dk' AND header_name NOT IN (
    SELECT header_name
    FROM the_table
    WHERE lang = 'dk'
)


I cant comment but all i want to ask is: In the example you just put up, the rows with Id 2 and ID 3 are the same entries only different language?

Id say you pull it out and make two tables

Example
id
sort
(all other generic columns)

example_translations
id
example_id
language_id
header_name
text

Then if querying for the danish translation of an example with id 1 it'll return the example_translations row of this entity. if it returns nothing you can query for the english version.

I dont think it is possible to do something like this on Mysql level


The way i understand this, you want to get the english content if the danish content is missing?.. You might want to add a column to your table where you mark your entries. (dont know if your "header_name" column does that efectly for you, i'm guessing that as well will be translated?..

Anyway, a column named "entry_id" where "tst dk" and "tst uk" would both have id "2" for an example, you should then when you load you manual ask for the "entry_id" and first look for the dk entry, and if it's not there, load the uk entry.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜