我的联接表中的一些结果是空的为什么?

wpcxdonn  于 2021-06-17  发布在  Mysql
关注(0)|答案(3)|浏览(330)

我有一个查询要搜索公司。每个公司都有一些存储在另一个表中的属性。
过帐到我的查询并且人们搜索时使用的值被调用 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的行)都是空的。为什么?
上面的查询给出了以下结果:

所有空字段的存储方式与 tags 但是 tags 显示其值,而其他值为空,为什么?
此外,所有字段都不是列名,它们都是别名,因为它们都存储为 value 它与 field_id 我需要检索所有数据,只需搜索( LIKE %% )内 tags .

q3qa4bjr

q3qa4bjr1#

你的案例陈述需要包含一个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
ruoxqz4g

ruoxqz4g2#

你说过只有标签应该包含“vlees”值。所以,不应该把它放到where,因为它过滤整个查询,而不是只过滤标记。我已经更新了你的查询,更改了“vlees”过滤器的位置。如果其他字段(hoofdaffeelding,openingstijden..)应该有另一个过滤器,你应该像我更新的标签一样过滤它们。最后一件事,我认为f.field\u 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
juzqafwq

juzqafwq3#

您正在筛选中的第二列 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%'

相关问题