Mysql 我的联接表中的某些结果为空,这是为什么?
我有一个查询要搜索公司。每个公司都有一些存储在另一个表中的属性 发布到我的查询中的值以及人们搜索时使用的值称为Mysql 我的联接表中的某些结果为空,这是为什么?,mysql,sql,database,Mysql,Sql,Database,我有一个查询要搜索公司。每个公司都有一些存储在另一个表中的属性 发布到我的查询中的值以及人们搜索时使用的值称为标记 这是我的疑问: SELECT cnt.id as content_id, cnt.title as content_title, cnt.featured, cnt.ordering, cnt.alias as content_alias, cnt.catid, cnt.images, cnt.state, cnt.introtext, MAX(case when f.field_
标记
这是我的疑问:
SELECT cnt.id as content_id, cnt.title as content_title, cnt.featured, cnt.ordering, cnt.alias as content_alias, cnt.catid, cnt.images, cnt.state, cnt.introtext,
MAX(case when f.field_id = 7 then f.value end) as hoofdafbeelding,
MAX(case when f.field_id = 8 then f.value end) as openingstijden,
MAX(case when f.field_id = 9 then f.value end) as straat,
MAX(case when f.field_id = 10 then f.value end) as facebook,
MAX(case when f.field_id = 11 then f.value end) as instagram,
MAX(case when f.field_id = 12 then f.value end) as telefoonnummer,
MAX(case when f.field_id = 13 then f.value end) as website,
MAX(case when f.field_id = 16 then f.value end) as tags
FROM snm_content cnt
LEFT JOIN snm_fields_values f
ON cnt.id = f.item_id
WHERE f.value LIKE '%vlees%'
GROUP BY cnt.id, cnt.title, cnt.featured, cnt.alias, cnt.catid, cnt.images, cnt.state, cnt.introtext
ORDER BY cnt.ordering
我的问题是在我的结果中,除标记外,所有字段(带MAX的行)都为空。为什么呢
上面的查询给出了以下结果:
所有空字段的存储方式与标记
完全相同,但是标记
显示其值,而其他字段为空,为什么
此外,所有字段都不是列名,它们都是别名,因为它们都存储为值
,该值与字段\u id
我需要检索所有数据,并且只在
标记中搜索(LIKE%%
)您正在对左连接中的第二列进行筛选。这是可疑的。通常的建议是将其移至on
子句:
FROM snm_content cnt LEFT JOIN
snm_fields_values f
ON cnt.id = f.item_id AND f.value LIKE '%vlees%'
另一方面,在本例中,我认为您可能根本不希望在查询中使用该条件
如果您确实希望搜索标记,则可以使用:
having tags like '%vlees%'
您的案例陈述需要包含else子句。此外,左外部联接可能返回空记录
SELECT cnt.id as content_id, cnt.title as content_title, ...
MAX(case when f.field_id = 7 then f.value else isnull({Column Name}, {Column Name}) end) as hoofdafbeelding,
MAX(case when f.field_id = 8 then f.value else isnull({Column Name}, {Column Name}) end) as openingstijden,
MAX(case when f.field_id = 9 then f.value else isnull({Column Name}, {Column Name}) end) as straat,
MAX(case when f.field_id = 10 then f.value else isnull({Column Name}, {Column Name}) end) as facebook,
MAX(case when f.field_id = 11 then f.value else isnull({Column Name}, {Column Name}) end) as instagram,
MAX(case when f.field_id = 12 then f.value else isnull({Column Name}, {Column Name}) end) as telefoonnummer,
MAX(case when f.field_id = 13 then f.value else isnull({Column Name}, {Column Name}) end) as website,
MAX(case when f.field_id = 16 then f.value else isnull({Column Name}, {Column Name}) end) as tags
您说过只有标记应该包含“vlees”值。所以,您不应该把它放在何处,因为它过滤整个查询,而不仅仅是标记。我已经更新了您的查询,更改了“vlees”过滤器的位置。如果其他字段(hoofdaffeelding、openingstijden..)应该有另一个过滤器,您应该像我更新的标签一样对它们进行过滤。
最后一件事,我认为f.field_id=?过滤器也不正确。使用此筛选器,您的结果案例将仅适用于一个content.id,您可能应该删除它们
SELECT
cnt.id AS content_id
,cnt.title AS content_title
,cnt.featured
,cnt.ordering
,cnt.alias AS content_alias
,cnt.catid
,cnt.images
,cnt.state
,cnt.introtext
,MAX(CASE WHEN f.field_id = 7 THEN f.value END) AS hoofdafbeelding
,MAX(CASE WHEN f.field_id = 8 THEN f.value END) AS openingstijden
,MAX(CASE WHEN f.field_id = 9 THEN f.value END) AS straat
,MAX(CASE WHEN f.field_id = 10 THEN f.value END) AS facebook
,MAX(CASE WHEN f.field_id = 11 THEN f.value END) AS instagram
,MAX(CASE WHEN f.field_id = 12 THEN f.value END) AS telefoonnummer
,MAX(CASE WHEN f.field_id = 13 THEN f.value END) AS website
,MAX(CASE WHEN f.field_id = 16 THEN f.value END) AS tags
FROM snm_content cnt
LEFT JOIN snm_fields_values f ON cnt.id = f.item_id
WHERE EXISTS (SELECT 1 FROM snm_fields_values SFV WHERE cnt.id = SFV.item_id AND SFV.value LIKE '%vlees%')
GROUP BY cnt.id
,cnt.title
,cnt.featured
,cnt.alias
,cnt.catid
,cnt.images
,cnt.state
,cnt.introtext
ORDER BY cnt.ordering
我看不懂那个小小的图像文字。。。请格式化文本,而不是图像。@twan您希望Hoofdaffeelding,openingstijden,straat。。所有计算字段都包含“vlees”?@ZekiGumus No only标记
如果我使用having而不是where(您上面的代码),我会得到以下错误:运行查询时出错[您的SQL语法有错误;请查看与您的MySQL服务器版本对应的手册,以了解可在第12行的“groupby cnt.id、cnt.title、cnt.featured、cnt.alias、cnt.catid、cnt.images、cnt”附近使用的正确语法]
@twan…HAVING
遵循分组依据
。上面的查询为我提供了所有公司的所有数据,因此这是很好的,除了它还显示了标签为空/空的公司。我只想在它们的tags@vlees我刚刚明白你需要什么。我已经用EXI更新了我的查询STS。