【发布时间】:2011-12-04 22:10:17
【问题描述】:
好的,由于代码发生了很大变化,我正在废弃旧版本。这是我现在拥有的:
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”。哦,是的,我只是在报废名称字段。那仍然行不通。 >.
【问题讨论】:
-
我看到的第一个问题是您无法决定表别名是
users2还是user2。 -
这几乎是完全无法理解的。。试着描述一下你想做的事情怎么样?
-
不,我的意思是,你想对查询做什么..
-
如果您在 SELECT 部分有字段但在 GROUP BY 中没有字段,则会看到错误。
标签: sql join left-join multiple-tables