带有 WHERE 条件和 INNER JOIN 的 SELECT 命令

发布于 2024-09-30 00:00:06 字数 1801 浏览 6 评论 0原文

我确信我一定在这里犯了一个小错误,但我一直在寻找有关此问题的帮助,而我所能找到的只是有关条件 INNER JOIN 的信息。

<强><编辑> 问题是这个存储过程根本不返回任何内容。 输入:

SELECT TOP (6) UserID, Category, Title, SUBSTRING(Article, 0, 200) AS Summary, DatePosted
FROM ContribContent
WHERE (DateFeatured IS NOT NULL)
ORDER BY DateFeatured DESC

如果我只在控制台中 然后我会得到返回的值。那么这一定与内连接有关吗? <强>< / EDIT>>

的思路是:

  1. 取已经编辑好的内容 精选(DateFeatured 不为 NULL) 并将其全部放入临时 table
  2. 从用户表中获取用户名和图片,并使用 UserID 值将它们与临时表中的值进行匹配。
  3. 按每个帖子的精选日期顺序对临时表进行排序。
  4. 从表中选择前六个条目

代码如下:

ALTER PROCEDURE [dbo].[admin_GetFeaturedContrib]
AS
BEGIN

DECLARE @FeaturedContrib TABLE (
 UserID INT,
 Category INT,
 Title varchar(100),
 Summary varchar(200),
 DatePosted date,
 FirstName varchar(50),
 LastName varchar(50),
 Picture varchar(100)
)

INSERT INTO @FeaturedContrib
SELECT TOP 6 ContribContent.UserID, ContribContent.Category, ContribContent.Title,     SUBSTRING(ContribContent.Article, 0, 200) AS Summary, ContribContent.DatePosted,     Users.FirstName, Users.LastName, Users.Picture
FROM ContribContent
INNER JOIN Users
ON ContribContent.UserID = Users.UserID
WHERE ContribContent.DateFeatured IS NOT NULL
ORDER BY ContribContent.DateFeatured DESC

SELECT * FROM @FeaturedContrib
END

涉及两个数据表:

用户 - 存储所有用户及其信息的表。

  • UserID INT
  • 名字 varchar(50)
  • 姓氏 varchar(50)
  • 图片 varchar(50)
  • 等...

ContribContent

  • ContribContentID INT
  • 用户 ID INT
  • 类别 INT
  • 标题 varchar(100)
  • 文章 varchar(MAX)
  • 图片 varchar(50)
  • DatePosted date
  • DateFeatured date
  • 已删除

感谢任何可以提供帮助的人!

I'm sure I must be making a trivial mistake here, but I've been searching around for help with this problem and all I can find is information on conditional INNER JOINs.

< EDIT > The problem is that this stored procedure is not returning anything at all. If I type just:

SELECT TOP (6) UserID, Category, Title, SUBSTRING(Article, 0, 200) AS Summary, DatePosted
FROM ContribContent
WHERE (DateFeatured IS NOT NULL)
ORDER BY DateFeatured DESC

Into the console then I get values returned. So it must be something to do with the inner-join? < / EDIT >

The idea is to:

  1. take the content which has been
    featured (DateFeatured is NOT NULL)
    and place it all into a temporary
    table
  2. get the user names and picture from the users table and match them to the values in the temporary table using the UserID value.
  3. sort the temporary table in order of the date each post was featured.
  4. select the top six entries from the table

Here's the code:

ALTER PROCEDURE [dbo].[admin_GetFeaturedContrib]
AS
BEGIN

DECLARE @FeaturedContrib TABLE (
 UserID INT,
 Category INT,
 Title varchar(100),
 Summary varchar(200),
 DatePosted date,
 FirstName varchar(50),
 LastName varchar(50),
 Picture varchar(100)
)

INSERT INTO @FeaturedContrib
SELECT TOP 6 ContribContent.UserID, ContribContent.Category, ContribContent.Title,     SUBSTRING(ContribContent.Article, 0, 200) AS Summary, ContribContent.DatePosted,     Users.FirstName, Users.LastName, Users.Picture
FROM ContribContent
INNER JOIN Users
ON ContribContent.UserID = Users.UserID
WHERE ContribContent.DateFeatured IS NOT NULL
ORDER BY ContribContent.DateFeatured DESC

SELECT * FROM @FeaturedContrib
END

There are two data tables involved:

Users - a table storing all of the users and their information.

  • UserID INT
  • FirstName varchar(50)
  • LastName varchar(50)
  • Picture varchar(50)
  • etc...

ContribContent

  • ContribContentID INT
  • UserID INT
  • Category INT
  • Title varchar(100)
  • Article varchar(MAX)
  • Picture varchar(50)
  • DatePosted date
  • DateFeatured date
  • Deleted bit

THANKS to anyone who can help out!

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

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

发布评论

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

评论(1

甜是你 2024-10-07 00:00:06

仅运行 -

SELECT TOP 6 ContribContent.UserID, ContribContent.Category, ContribContent.Title,     SUBSTRING(ContribContent.Article, 0, 200) AS Summary, ContribContent.DatePosted,     Users.FirstName, Users.LastName, Users.Picture
FROM ContribContent
INNER JOIN Users
ON ContribContent.UserID = Users.UserID
WHERE ContribContent.DateFeatured IS NOT NULL
ORDER BY ContribContent.DateFeatured DESC

查看您得到的内容可能是您的 Wherejoin 的问题,请仔细查看是否首先返回了任何数据。我的猜测是 join 看看您是否有匹配的用户 ID 要加入...(提示:Left join 也许是您的答案)

Run only -

SELECT TOP 6 ContribContent.UserID, ContribContent.Category, ContribContent.Title,     SUBSTRING(ContribContent.Article, 0, 200) AS Summary, ContribContent.DatePosted,     Users.FirstName, Users.LastName, Users.Picture
FROM ContribContent
INNER JOIN Users
ON ContribContent.UserID = Users.UserID
WHERE ContribContent.DateFeatured IS NOT NULL
ORDER BY ContribContent.DateFeatured DESC

See what you getting might be an issue with your Where or your join see carefully if you have any data in the first place being returned. My guess is join see if you have matching userids you are joining on...(Hint : Left join maybe your answer)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文