简单的Sql查询:查询问题 [英] Simple Sql Query :Problem with query
问题描述
下面是查询我被困在怎么办
sno | smanid | sid
------------------
1 | 5 | 4
2 | 5 | 2
3 | 6 | 7
4 | 3 | 5
5 | 6 | 3
smanid是使用sid制作的产品
smanid 5由sid 4和2制成
smanid 6由sid 7和3制作/>
现在给产品是,sid我想知道smanid
例如:给定4和2给出5
给出7和3给出6
我怎样才能为我写出查询
below is query i am stuck with how to do
sno |smanid | sid
------------------
1 |5 |4
2 |5 |2
3 |6 |7
4 |3 |5
5 |6 |3
smanid is product made using sid
smanid 5 is made from sid 4 and 2
smanid 6 is made from sid 7 and 3
now give product is, sid i want to know smanid
eg : given 4 and 2 gives 5
given 7 and 3 gives 6
how can i write query for that i am stuced
推荐答案
我不确定你的问题是什么 - 当然它只是
Im not sure what your issue is - surely its just
select smanid from yourtable where sid = yourvalue
其中'yourtable'应该是你的表的真实姓名,你的值是4或2或7或3你想要
where 'yourtable' should be the real name of your table and yourvalue is 4 or 2 or 7 or 3 as you want
一世我不确定你想要实现什么,但听起来你想将行连接成一行 [ ^ ],它将为您提供如下结果:
I'm not sure what you want to achieve, but it sounds like you want to concatenate rows into one row[^], which will give you result like:
5 | 4,2
etc.
SELECT DISTINCT smanid, STUFF((SELECT ','+ CONVERT(VARCHAR(30), sid)
FROM [YourTable] AS S2
WHERE S1.smanid = S2.smanid
FOR XML PATH('')), 1, 1, '') AS Sids
FROM [YourTable] AS S1
如果传递2个id值是静态的,你可以尝试这个,
选择不同表中的smanid,其中sid = value1和sid = value2
除此之外你可以根据需要使用和,或。
If passing 2 id values are static,u can try this,
select distinct smanid from table where sid=value1 and sid=value2
apart from this u can use "and,or" as per your needs.
>
这篇关于简单的Sql查询:查询问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!