0

在查看控制器中的一些代码时,我发现查询中有一个奇怪的错误,基本上是在语句的情况下:

数据库内容:

列 :

category : client or case category
type : title or content
mainpage 1 or 0 (true or false)

价值观:

0;0;1

对应正常输出:

Case category;title;yes

电流输出:

Client;Content;No

PHQL:

SELECT content.id,
(CASE content WHEN content.category = 0 THEN 'Case category' WHEN content.category = 1 THEN 'Client' WHEN content.category = 2 THEN 'Case' WHEN content.category = 3 THEN 'Product' WHEN content.category = 4 THEN 'Product category' WHEN content.category = 5 THEN 'Team' WHEN content.category = 6 THEN 'Vacancy' WHEN content.category = 7 THEN 'Reference' WHEN content.category = 8 THEN 'Article' ELSE "Category not found" END) AS category 

FROM Apps\Source\Models\MainContent AS content 
GROUP BY content.id 

事实是所有的“case when”语句都返回了错误的值(例如:数据库中的 content.category = 0,但它让我得到了“客户端事件”,它应该得到它的值为 1)

如果我将语句翻译成纯 sql,只是为了测试它,它可以工作......

SELECT 
content.id,
(CASE WHEN content.category = 0 THEN 'Case category' WHEN content.category = 1 THEN 'Client' WHEN content.category = 2 THEN 'Case' WHEN content.category = 3 THEN 'Product' WHEN content.category = 4 THEN 'Product category' WHEN content.category = 5 THEN 'Team' WHEN content.category = 6 THEN 'Vacancy' WHEN content.category = 7 THEN 'Reference' WHEN content.category = 8 THEN 'Article' ELSE "Category not found" END) AS category 

FROM main_content AS content 
GROUP BY content.id 

输出 :

Case category;title;yes

这是 phql 的怪事吗?

4

1 回答 1

0

两种语句之间存在差异(原始sql是搜索case,PHQL很简单):在PHQL中尝试

SELECT content.id,
(CASE WHEN ...

这么多案例,做一个“Category”查找表并加入“main_content”不是更好吗?

于 2018-09-13T20:17:24.340 回答