带有 WHERE 条件和 INNER JOIN 的 SELECT 命令
我确信我一定在这里犯了一个小错误,但我一直在寻找有关此问题的帮助,而我所能找到的只是有关条件 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>>
的思路是:
- 取已经编辑好的内容 精选(DateFeatured 不为 NULL) 并将其全部放入临时 table
- 从用户表中获取用户名和图片,并使用 UserID 值将它们与临时表中的值进行匹配。
- 按每个帖子的精选日期顺序对临时表进行排序。
- 从表中选择前六个条目
代码如下:
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:
- take the content which has been
featured (DateFeatured is NOT NULL)
and place it all into a temporary
table - get the user names and picture from the users table and match them to the values in the temporary table using the UserID value.
- sort the temporary table in order of the date each post was featured.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅运行 -
查看您得到的内容可能是您的
Where
或join
的问题,请仔细查看是否首先返回了任何数据。我的猜测是join
看看您是否有匹配的用户 ID 要加入...(提示:Left join 也许是您的答案)Run only -
See what you getting might be an issue with your
Where
or yourjoin
see carefully if you have any data in the first place being returned. My guess isjoin
see if you have matching userids you are joining on...(Hint : Left join maybe your answer)