0

这个问题与:

ON 子句中的另一个未知列名错误

我的实际查询:

    SELECT  c.title AS title
      , c.introtext AS body
      , c.state AS state
      , c.created AS created
      , c.created_by AS uid
      , c.modified AS modified
      , c.modified_by AS modified_uid
      , c.published AS published
      , c.published_by AS published_uid
      , jc.title AS category
    FROM  jos_content AS c
    INNER JOIN jos_categories AS jc 
    ON c.sectionid = jc.section 
    WHERE c.sectionid = 4

我想按部分和该内容的类别名称获取内容。

我知道在 jos_content 表中,id 为 4 部分的行是 8000,但是这个查询返回我大约 177k 行。

我尝试将 INNER JOIN 更改为 LEFT 等并使用 DISTINCT 但它无济于事

表格列:

jos_content:    

id, title, introtext, state, created, created_by etc


jos_categories:

  id, section (id of sections, names doesn't naming convention IMO, its Joomla 1.5 db BTW), title

jos_sections:
  id, title

我想要得到的是:

jos_content.title (etc) 和所选部分的 jos_categories_name

4

2 回答 2

1

由于缺乏信息,我无法告诉您如何编写查询,但我可以告诉您为什么您在现有查询上得到错误的结果。

如果这是您的表格的内容(简化);

jos_content:    id      sectionid      title
                 1              4      Content 1 
                 2              4      Content 2

jos_categories  id        section      title
                 1              4      Category 1
                 2              4      Category 2

...然后您运行查询(简化)...

SELECT  c.title AS title
     , jc.title AS category
FROM  jos_content AS c
INNER JOIN jos_categories AS jc 
ON c.sectionid = jc.section 
WHERE c.sectionid = 4

...结果,您将获得 4 行(第 4 节中的类别数乘以第 4 节中的内容项数)。

title        category
Content 1    Category 1
Content 2    Category 1
Content 1    Category 2
Content 2    Category 2

原因是查询中没有将内容绑定到类别的任何内容,您正在通过部分进行查询,该部分为您的查询提供了许多可能的答案。

于 2012-08-11T08:31:16.867 回答
0

或者,试试这个 script.Group by [SectionId] 设置中的 [Content] 表:

SELECT  c.title AS title
      , c.introtext AS body
      , c.state AS state
      , c.created AS created
      , c.created_by AS uid
      , c.modified AS modified
      , c.modified_by AS modified_uid
      , c.published AS published
      , c.published_by AS published_uid
      , jc.title AS category
    FROM  jos_content AS c
    INNER JOIN jos_categories AS jc 
    ON c.sectionid = jc.section and c.sectionid=4
    gorup by c.sectionid 
于 2012-08-11T08:19:04.417 回答