Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 为什么使用此查询时我的结果为空_Mysql_Sql_Database - Fatal编程技术网

Mysql 为什么使用此查询时我的结果为空

Mysql 为什么使用此查询时我的结果为空,mysql,sql,database,Mysql,Sql,Database,我得到的产品和一个类别,该产品属于太显示在页面上。此外,我还获得了属于另一个表中的产品的一些数据 我的查询如下所示: SELECT cnt.id as content_id, cnt.title as content_title, cnt.featured, cnt.alias as content_alias, cnt.catid, cnt.images, cnt.state, cnt.introtext, cat.id as cat_id, cat.title as cat_title, c

我得到的产品和一个类别,该产品属于太显示在页面上。此外,我还获得了属于另一个表中的产品的一些数据

我的查询如下所示:

SELECT cnt.id as content_id, cnt.title as content_title, cnt.featured, cnt.alias as content_alias, cnt.catid, cnt.images, cnt.state, cnt.introtext, cat.id as cat_id, cat.title as cat_title, cat.alias as cat_alias,
MAX(case when f.field_id = 4 then f.value end) as prijs,
MAX(case when f.field_id = 5 then f.value end) as prijsoud
FROM snm_fields_values f
JOIN snm_content cnt
ON cnt.id = f.item_id
JOIN snm_categories cat
ON cnt.catid = cat.id
WHERE cnt.catid = '17'
AND cnt.state = 1
GROUP BY f.item_id
我的问题是,当分类下没有任何项目时,所有结果都是空的。因此,在没有属于该类别的产品的类别页面上,它也不会显示类别标题

只有当类别下有产品(snm_内容)时,才会显示所有数据

上述查询返回以下内容:

catid 17下没有任何产品

当我将其更改为16(其中有产品)时,这是我的结果:

我想获得所有的数据,所以当一个类别没有产品时,我仍然需要类别标题


当一个产品不存在时,为什么所有的东西都是空的?

我想你需要外部连接。我不完全理解
分组依据
,但这可能会满足您的要求:

SELECT cnt.id as content_id, cnt.title as content_title, cnt.featured, cnt.alias as content_alias, cnt.catid, cnt.images, cnt.state, cnt.introtext, cat.id as cat_id, cat.title as cat_title, cat.alias as cat_alias,
       MAX(case when f.field_id = 4 then f.value end) as prijs,
       MAX(case when f.field_id = 5 then f.value end) as prijsoud
FROM snm_categories cat LEFT JOIN
     snm_content cnt
     ON cnt.catid = cat.id AND cnt.state = 1 LEFT JOIN
     snm_fields_values f 
     ON cnt.id = f.item_id 
WHERE cat.id = 17
GROUP BY cnt.id, cnt.title, cnt.featured, cnt.alias, cnt.catid, cnt.images, cnt.state, cnt.introtext, cat.id, cat.title, cat.alias

看看左右连接。当子表中不存在行时,它们将包括父表中的行。因此,您需要使用左外部联接或右外部联接来返回非相等数据。请检查()@Sloarchasher,我尝试使用这两种方法,但不管怎样,我都会得到一个空结果。我尝试了左/右连接和左外/右外连接,但没有结果。请正确使用
groupby
。它甚至不是有效的
分组依据。所有这些未聚合的列都必须位于
GROUP BY
子句中。cat.catid必须是cnt.catid,但当我使用它时,仍然会得到一个空结果。@twan。它应该是
cat.id