我有一张包含语言的表格。
表格
lan_dish_id | lan_dish_lang | lan_dish_dishid | lan_dish_mainlang
--------------- | ------------------- | --------------------- | ------------------------
1 | nl | 8 | 1
4 | fr | 8 | 0
我需要检查表中是否有$_SESSION['lang']
中的语言,否则它必须采用主语言。
不幸的是,当运行以下查询时,他返回带FR的行以及NL als lan_dish_lan ...在这种情况下,它只返回带有FR的行。
SELECT * FROM `language_dish` WHERE
(CASE
WHEN lan_dish_lang = "fr"
THEN lan_dish_lang = "fr"
ELSE lan_dish_mainlang = 1
END) AND lan_dish_dishid = 8
答案 0 :(得分:1)
这里有两种可能对你有用的方法。
/*
DROP TABLE IF EXISTS T;
CREATE TABLE T (lan_dish_id INT, lan_dish_lang VARCHAR(2), lan_dish_dishid INT, lan_dish_mainlang INT);
*/
truncate table t;
INSERT INTO T VALUES
(1 , 'nl' , 8, 1) ,
(4 , 'fr' , 8, 0) ,
(5 , 'nl' , 9, 1) ,
(6 , 'de' , 7, 0)
;
select s.lan_dish_dishid, left(gc,instr(concat(gc,','),',') -1) lang
from
(
select lan_dish_dishid, group_concat(lan_dish_lang order by lan_dish_mainlang) gc
from t
#where lan_dish_dishid = 8
group by lan_dish_dishid
order by lan_dish_dishid
) s
;
select s.lan_dish_id, s.lan_dish_lang
from
(
select t.*,
if(t.lan_dish_dishid <> @p , @rn:=1,@rn:=@rn+1) rn,
@p:= t.lan_dish_dishid p
from t , (select @rn:=0,@p:=0) r
order by t.lan_dish_dishid, t.lan_dish_Mainlang
) s where s.rn = 1
结果
+-------------+---------------+
| lan_dish_id | lan_dish_lang |
+-------------+---------------+
| 6 | de |
| 4 | fr |
| 5 | nl |
+-------------+---------------+
3 rows in set (0.01 sec)