我该如何执行这个荒谬的 SQL 多重连接语句?

发布于 2024-12-09 03:57:46 字数 1619 浏览 0 评论 0原文

好吧,我正在废弃旧的,因为代码已经改变了很多。这是我现在所拥有的:

SELECT
   article.articleId,
   article.articleName,
   article.articleStoryTypeId,
   artIndustry.industryName,
   author.userPrefix,
   author.userMiddleName,
   article.articleStatus,
   article.articleCreateDate,
   article.articleModifyDate
FROM
   users
LEFT JOIN clickTrack
 ON users.userId = clickTrack.clickUserId
LEFT JOIN article
 ON clickTrack.clickDocumentId = article.articleId
LEFT JOIN users author
 ON author.userId = article.articleAuthorId
LEFT JOIN industry artIndustry
 ON artIndustry.industryId =    substring(article.articleIndustryId,0,charindex(',',article.articleIndustryId)-1)
WHERE ((clickTrack.clickDocumentTable = 'breaking')
     OR (clickTrack.clickDocumentTable = 'article'))
  AND article.articleCreateDate > '1/1/2008 0:00:00 AM'
  AND ((users.userindustryId = '1') 
    OR (users.userindustryId LIKE '%,1') 
    OR (users.userindustryId LIKE '%,1,%') 
    OR (users.userindustryId LIKE '1,%'))
GROUP BY
   article.articleId,
   article.articleName,
   article.articleStoryTypeId,
   artIndustry.industryName,
   author.userPrefix,
   author.userMiddleName,
   article.articleStatus,
   article.articleCreateDate,
   article.articleModifyDate
Order By article.articleId

返回的数据如下所示:

8332,理想情况,突发新闻,NULL,NULL,NULL,Prod,2011-07-25 14:48:01.203,2011-08-09 07:41:29.373

我添加了逗号。 3 个空字段是行业名称、用户的名字和用户的姓氏。 (是的,有人将表字段命名为错误。)我非常累,所以我不知道这是否是足够的信息。如果您需要更多,请告诉我。

编辑

它现在正在工作。我必须从左连接子句中去掉“-1”。哦,是的,我只是废弃名称字段。那还是不行。 >。<但我已经完成了。

Ok, I'm scrapping the old since the code has changed a lot. Here's what I now have:

SELECT
   article.articleId,
   article.articleName,
   article.articleStoryTypeId,
   artIndustry.industryName,
   author.userPrefix,
   author.userMiddleName,
   article.articleStatus,
   article.articleCreateDate,
   article.articleModifyDate
FROM
   users
LEFT JOIN clickTrack
 ON users.userId = clickTrack.clickUserId
LEFT JOIN article
 ON clickTrack.clickDocumentId = article.articleId
LEFT JOIN users author
 ON author.userId = article.articleAuthorId
LEFT JOIN industry artIndustry
 ON artIndustry.industryId =    substring(article.articleIndustryId,0,charindex(',',article.articleIndustryId)-1)
WHERE ((clickTrack.clickDocumentTable = 'breaking')
     OR (clickTrack.clickDocumentTable = 'article'))
  AND article.articleCreateDate > '1/1/2008 0:00:00 AM'
  AND ((users.userindustryId = '1') 
    OR (users.userindustryId LIKE '%,1') 
    OR (users.userindustryId LIKE '%,1,%') 
    OR (users.userindustryId LIKE '1,%'))
GROUP BY
   article.articleId,
   article.articleName,
   article.articleStoryTypeId,
   artIndustry.industryName,
   author.userPrefix,
   author.userMiddleName,
   article.articleStatus,
   article.articleCreateDate,
   article.articleModifyDate
Order By article.articleId

Data returned looks like:

8332, The Ideal Situation, Breaking News, NULL, NULL, NULL, Prod, 2011-07-25 14:48:01.203, 2011-08-09 07:41:29.373

I added the commas. The 3 null fields are the industry name, user's first name, and user's last name. (Yes, someone named the table fields wrong.) I'm extremely tired, so I don't know if this is enough info or not. Just let me know if you need more.

EDIT

It's working now. I had to take the "-1" out of the left join on clause. Oh yeah, and I'm just scrapping the name fields. That's still not working. >.< But I'm done.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小鸟爱天空丶 2024-12-16 03:57:46

作者列表和用户列表真的是同一张表吗?您想查询什么?

一个问题是您试图返回不在您的分组依据中的字段。

试试这个:

SELECT
   article.articleId,
   article.articleName,
   article.articleStoryTypeId,
   artIndustry.industryName,
   author.userPrefix,
   author.userMiddleName,
   article.articleStatus,
   article.articleCreateDate,
   article.articleModifyDate,
FROM
   users
LEFT JOIN clickTrack
 ON users.userId = clickTrack.clickUserId
LEFT JOIN article
 ON clickTrack.clickDocumentId = article.articleId
LEFT JOIN users author
 ON author.userId = article.articleAuthorId
LEFT JOIN industry artIndustry
 ON artIndustry.industryId = article.articleIndustryId
WHERE ((clickTrack.clickDocumentTable = 'breaking')
     OR (clickTrack.clickDocumentTable = 'article'))
  AND article.articleCreateDate > '1/1/2008 0:00:00 AM'
  AND users.industryId = 1
GROUP BY
   article.articleId,
   article.articleName,
   article.articleStoryTypeId,
   artIndustry.industryName,
   author.userPrefix,
   author.userMiddleName,
   article.articleStatus,
   article.articleCreateDate,
   article.articleModifyDate,
Order By article.articleId

Is the author list really the same table as the user list? What are you trying to query for?

One problem is that you are trying to return fields that aren't in your group by.

Try this:

SELECT
   article.articleId,
   article.articleName,
   article.articleStoryTypeId,
   artIndustry.industryName,
   author.userPrefix,
   author.userMiddleName,
   article.articleStatus,
   article.articleCreateDate,
   article.articleModifyDate,
FROM
   users
LEFT JOIN clickTrack
 ON users.userId = clickTrack.clickUserId
LEFT JOIN article
 ON clickTrack.clickDocumentId = article.articleId
LEFT JOIN users author
 ON author.userId = article.articleAuthorId
LEFT JOIN industry artIndustry
 ON artIndustry.industryId = article.articleIndustryId
WHERE ((clickTrack.clickDocumentTable = 'breaking')
     OR (clickTrack.clickDocumentTable = 'article'))
  AND article.articleCreateDate > '1/1/2008 0:00:00 AM'
  AND users.industryId = 1
GROUP BY
   article.articleId,
   article.articleName,
   article.articleStoryTypeId,
   artIndustry.industryName,
   author.userPrefix,
   author.userMiddleName,
   article.articleStatus,
   article.articleCreateDate,
   article.articleModifyDate,
Order By article.articleId
何以笙箫默 2024-12-16 03:57:46

啊,好吧,试试这个:

SELECT 
  article.articleId,
  article.articleName, 
  article.articleStoryTypeId, 
  a.industryName, 
  a.userPrefix, 
  a.userMiddleName, 
  article.articleStatus, 
  article.articleCreateDate, 
  article.articleModifyDate,
  count(*)
FROM (select * from users
      INNER JOIN industry ON industry.industryId = users.userIndustryId 
                          AND industry.industryId = 1) a
LEFT OUTER JOIN clickTrack ON users.userId = clickTrack.clickUserId 
                     AND clickTrack.clickDocumentTable IN ('breaking','article')
LEFT OUTER JOIN article ON clickTrack.clickDocumentId = article.articleId 
                  AND article.articleCreateDate > '1/1/2008 0:00:00 AM' 
GROUP BY 
  article.articleId,
  article.articleName, 
  article.articleStoryTypeId, 
  a.industryName, 
  a.userPrefix, 
  a.userMiddleName, 
  article.articleStatus, 
  article.articleCreateDate, 
  article.articleModifyDate
ORDER BY 
  article.articleId

Ah, ok, try this:

SELECT 
  article.articleId,
  article.articleName, 
  article.articleStoryTypeId, 
  a.industryName, 
  a.userPrefix, 
  a.userMiddleName, 
  article.articleStatus, 
  article.articleCreateDate, 
  article.articleModifyDate,
  count(*)
FROM (select * from users
      INNER JOIN industry ON industry.industryId = users.userIndustryId 
                          AND industry.industryId = 1) a
LEFT OUTER JOIN clickTrack ON users.userId = clickTrack.clickUserId 
                     AND clickTrack.clickDocumentTable IN ('breaking','article')
LEFT OUTER JOIN article ON clickTrack.clickDocumentId = article.articleId 
                  AND article.articleCreateDate > '1/1/2008 0:00:00 AM' 
GROUP BY 
  article.articleId,
  article.articleName, 
  article.articleStoryTypeId, 
  a.industryName, 
  a.userPrefix, 
  a.userMiddleName, 
  article.articleStatus, 
  article.articleCreateDate, 
  article.articleModifyDate
ORDER BY 
  article.articleId
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文