在查看控制器中的某些代码时,我发现查询中存在一个怪异的错误,基本上是在when语句的情况下:
数据库内容:
列:
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奇怪吗?
答案 0 :(得分:0)
这两个语句之间有区别(搜索原始sql,PHQL很简单):在PHQL中尝试
SELECT content.id,
(CASE WHEN ...
在很多情况下,制作“类别”查找表并将其与“ main_content”结合起来会更好吗?