我有一个查询来搜索公司。每个公司都有一些属性,这些属性存储在另一个表中。
发布到我的查询中并且人们用来搜索的值称为tags
。
这是我的查询
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的行)均为NULL。为什么会这样?
上面的查询给了我这个结果:
所有NULL字段的存储方式与tags
完全相同,但是tags
显示其值,而其他字段为NULL,为什么?
所有字段也不是列名,它们是别名,因为它们都存储为与value
连接的field_id
我需要检索所有数据,并且只能在LIKE %%
内搜索(tags
)。
答案 0 :(得分:1)
您说过,只有标签应包含'vlees'值。因此,您不应将其放到WHERE,因为它会过滤整个查询而不是仅过滤标记。我已经更新了您的查询,更改了“ vlees”过滤器的位置。如果其他字段(hoofdafbeelding,openingstijden ..)应该具有另一个过滤器,则应像我更新的标签一样过滤它们。 最后一件事,我认为f.field_id =?过滤器也不正确。使用此过滤器,您的结果CASE仅适用于一个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
答案 1 :(得分:0)
您正在left join
的第二列中进行过滤。这是可疑的。通常的建议是将其移至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%'
答案 2 :(得分:0)
您的案例陈述需要包含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