我想在表格中创建一个额外的列。目的是给所有行的邮政编码在2900和3199或2600和2699之间的值为“Omgeving Rotterdam”,所有其他邮政编码为“Nederland”,所有行的空单元格为“No Postcode”。
我尝试了下面的代码(但是它给了我“在添加的行的所有单元格中的Omgeving鹿特丹”)
SELECT *
,case
when (LEFT(Postcode,4) between 2900 AND 3199 OR 2600 AND 2699) then 'Omgeving Rotterdam'
when (LEFT(Postcode,4) IS NOT NULL AND NOT between 2900 AND 3199 OR 2600 AND 2699) then 'Nederland'
else 'Geen Postcode'
end
as 'Komt Uit'
FROM
a
LEFT JOIN
b ON a.ID = b.code
JOIN
c ON c.id_E = a.E_ID
答案 0 :(得分:2)
这不是答案。仅供参考:
class A:baseA
{
public:
void task(void);
void fnc1(void);
void (B::*fnc2)(void)=NULL;
B* b;
};
void A::task()
{
if(this->fnc2!=NULL && b != NULL)
(b->*fnc2)(); // note the parentheses to avoid a syntax error
}
class B
{
public:
void task(void);
void fnc1(void);
void (baseA::*fnc2)(void);
A* a;
};
void B::task(void)
{
if(this->fnc2!=NULL && a != NULL)
(a->*fnc2)();
}
是
when (LEFT(Postcode,4) between 2900 AND 3199 OR 2600 AND 2699) then 'Omgeving Rotterdam'
2600不是真正的条件,2699也不是。但MySQL推测某些条件会将整数转换为布尔值(使0 = false,其他数字= true)。所以你有:
when LEFT(Postcode,4) between 2900 AND 3199
OR (2600 AND 2699) then 'Omgeving Rotterdam'
仅仅是
when LEFT(Postcode,4) between 2900 AND 3199
OR (true AND true) then 'Omgeving Rotterdam'
这就是为什么你的所有成绩都是'Omgeving Rotterdam'。
至于:
when true then 'Omgeving Rotterdam'
我认为应该引发错误。我不知道mySQL的解析器对when (LEFT(Postcode,4) IS NOT NULL AND NOT between 2900 AND 3199 OR 2600 AND 2699) then 'Nederland'
做了什么。
答案 1 :(得分:1)
所以我认为你需要完全指定每个AND / OR步骤,结果如下:
SELECT *
,case
when (LEFT(Postcode,4) between 2900 AND 3199 OR LEFT(Postcode,4) between 2600 AND 2699) then 'Omgeving Rotterdam'
when (LEFT(Postcode,4) IS NOT NULL AND LEFT(Postcode,4) NOT between 2900 AND 3199 OR LEFT(Postcode,4) NOT between 2600 AND 2699) then 'Nederland'
else 'Geen Postcode'
end
as 'Komt Uit'
FROM
a
LEFT JOIN
b ON a.ID = b.code
JOIN
c ON c.id_E = a.E_ID
答案 2 :(得分:1)
据推测,postcode
是字符串,而不是数字。你不应该混合你的类型。它可能效率低下,有时会导致效率低下。
在您的情况下,您正在使用LEFT()
,但实际上并不需要相同长度的字符串。所以这可能会做你想要的:
select *,
(case when (Postcode >= '2900' and Postcode < '3200') or
(Postcode >= '2600' and Postcode <
'2700')
then 'Omgeving Rotterdam'
when post is null
then 'Geen Postcode'
else 'Nederland'
end) as Komt_Uit
from a join
c
on c.id_E = a.E_ID left join
b
on a.ID = b.code;
注意:
postcode
确实有可变长度,那么您可能需要left()
,但您可能需要检查数据。postcode
是数字,请使用数字操作或将显式转换为字符串。left join
和inner join
时,我总是先放置inner join
。