使用sql server查询获取描述 [英] using sql server query get the description
问题描述
表名studentdetails
studid课程代码说明
1000 AFF高级消防
2000 MC medicare < br $>
表名SMSDetails
studid关键字Msgdelivered
1000 HIMT AFF是
2000 HIMT MC是
使用上面的两个表我想得到描述(来自studentdetails表)和Msgdelivered(来自SMSDetails表)。
为此我写了如下查询
选择s.studid,s.Description,c.Msgdelivered来自studentdetails s,SMSDetails c其中s.studid = c.studid和s.coursecode = RIGHT(c.keyword) ,4)
当我运行以上查询显示输出如下时,没有任何输出即将来临
studid description MSgdelivered
但是当我执行以下查询时如下
select s.studid ,s.Description,c.Msgdelivered from studentdetails s,SMSDetails c其中s.studid = c.studid
studid description MSgdelivered
1000高级消防是
2000医疗保险是
选择s.studid,s.Description,c。 Msgdelivered来自studentdetails s,SMSDetails c其中s.studid = c.studid和s.coursecode = RIGHT(c.keyword,4)
请问有什么问题我上面的查询使用上面的查询在右边修剪关键字从SMSdetails表有什么问题。
问候,
Narasiman P.
Table name studentdetails
studid coursecode Description
1000 AFF advanced fire fighting
2000 MC medicare
Table name SMSDetails
studid keyword Msgdelivered
1000 HIMT AFF yes
2000 HIMT MC yes
using above two table i want to get the Description (from studentdetails table) and Msgdelivered (from SMSDetails table).
for that i written the query as follows
select s.studid,s.Description,c.Msgdelivered from studentdetails s,SMSDetails c where s.studid=c.studid and s.coursecode= RIGHT(c.keyword,4)
When i run the above query shows output as follows nothing no output is coming
studid description MSgdelivered
But when i execute the below query as follows
select s.studid,s.Description,c.Msgdelivered from studentdetails s,SMSDetails c where s.studid=c.studid
studid description MSgdelivered
1000 advanced fire fighting Yes
2000 medicare Yes
select s.studid,s.Description,c.Msgdelivered from studentdetails s,SMSDetails c where s.studid=c.studid and s.coursecode= RIGHT(c.keyword,4)
please what is the problem in my above query using above query in Right trim the keyword from SMSdetails table what is the problem.
Regards,
Narasiman P.
推荐答案
你需要使用JOIN
试试这个:
You need to use a JOIN
Try this:
SELECT sd.studid, sd.Description, smd.Msgdelivered
FROM SMSDetails smd
JOIN studentdetails sd
ON SUBSTRING(smd.keyword, 6, 3)=sd.coursecode
应该做你想做的事。
根据你的数据,你必须使用RIGHT(c.keyword,3)而不是RIGHT(c.keyword,4),否则使用ltrim(右(c.keyword,4))修剪左侧空间
As per your data you have to use RIGHT(c.keyword,3) instead of RIGHT(c.keyword,4), or else use ltrim(RIGHT(c.keyword,4)) to trim the left hand side space
这篇关于使用sql server查询获取描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!