Mysql SQL where子句依赖于prevoius select语句
在下面的声明中,我试图从数据库中检索推特及其作者信息。我还想检索与这些tweet相关的hashtag(换句话说,在这些tweet中提到)Mysql SQL where子句依赖于prevoius select语句,mysql,Mysql,在下面的声明中,我试图从数据库中检索推特及其作者信息。我还想检索与这些tweet相关的hashtag(换句话说,在这些tweet中提到) 除了如何在Tweet和Hashtag之间链接之外,一切都很好。我想首先检索tweets及其作者,并根据检索到的tweets检索写在这些tweets中的hashtag。目前,我无法修复的错误是where子句where TweetID=TID中的第二个select语句。我试图让第二条语句根据第一条语句中检索到的tweet ID获取tweet ID,但没有成功 您的
除了如何在Tweet和Hashtag之间链接之外,一切都很好。我想首先检索tweets及其作者,并根据检索到的tweets检索写在这些tweets中的hashtag。目前,我无法修复的错误是where子句
where TweetID=TID
中的第二个select语句。我试图让第二条语句根据第一条语句中检索到的tweet ID获取tweet ID,但没有成功 您的查询有许多问题,无法运行
可能您正在寻找这样的查询:
select Tweet.Label,
Tweet.TDate,
Author.Lable,
Author.ALink,
Tweet.TLink,
Tweet.ID,
Hashtag.Label,
Hashtag.HLink
from Tweet
inner join Author
on Tweet.AuhtorID = Author.ID
left join TweetHashs
on TweetHashs.TweetID = Tweet.ID
left join Hashtag
on Hashtag.ID = TweetHashs.HashID
where Tweet.ID IN
( select TweetID
from TweetMention
where MentionID IN
( select ID
from Mention
where Label = 'Cr7Prince4ever'
)
)
注:TweetHash的拼写似乎是错误的,但你是这么说的
上面的查询将为同一条tweet返回多个结果,因为它在单独的一行中列出了一条tweet的每个哈希值。如果您希望每条tweet只有一个结果,并且将它们的散列连接在一个逗号分隔的列表中,那么您可以使用group\u concat
和group by
:
select Tweet.Label,
Tweet.TDate,
Author.Lable,
Author.ALink,
Tweet.TLink,
Tweet.ID,
group_concat(Hashtag.Label) labels,
group_concat(Hashtag.HLink) links
from Tweet
inner join Author
on Tweet.AuhtorID = Author.ID
left join TweetHashs
on TweetHashs.TweetID = Tweet.ID
left join Hashtag
on Hashtag.ID = TweetHashs.HashID
where Tweet.ID IN
( select TweetID
from TweetMention
where MentionID IN
( select ID
from Mention
where Label = 'Cr7Prince4ever'
)
)
group by Tweet.ID
请先指定是sql server还是mysql。sql server是Microsoft sql server,而不是通用sql server。第二,指定您正在使用的数据库的版本。让我列举一些错误:(1)您有两个完全不相关的数据库作为标记;(2) 您使用的是过时的联接语法(应该禁止在
from
子句中使用逗号);(3) 您正在对具有不同列的两个子查询执行联合
;(4) 您可能正在尝试在where
子句(TID
)中使用列别名。我建议你删除这个问题,然后问另一个问题,包括样本数据、期望的结果、对你想要的内容的清晰解释,并且不要声称一个错误的查询“工作正常”。这回答了你的问题吗?你能留下评论吗?
select Tweet.Label,
Tweet.TDate,
Author.Lable,
Author.ALink,
Tweet.TLink,
Tweet.ID,
group_concat(Hashtag.Label) labels,
group_concat(Hashtag.HLink) links
from Tweet
inner join Author
on Tweet.AuhtorID = Author.ID
left join TweetHashs
on TweetHashs.TweetID = Tweet.ID
left join Hashtag
on Hashtag.ID = TweetHashs.HashID
where Tweet.ID IN
( select TweetID
from TweetMention
where MentionID IN
( select ID
from Mention
where Label = 'Cr7Prince4ever'
)
)
group by Tweet.ID