如何检索行. [英] How to retrieve rows.

查看:98
本文介绍了如何检索行.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好......
我有两个表table1和table2,table1有两个列ID,名称,并且table1中有三个记录
编号名称
7 abc
96 pqr
129 xyz
table1 id保存在table2s mast_id中,例如7 + 96 + 129
table2也有两个列ID,mast_id和一个记录init
id mast_id
1 7 + 96 + 129

如果我检索了table2行,它将像7,96和129这样分隔mast_id.

Hello....
i have two table table1 and table2, table1 have two coloumn id,name and there are three records in table1
id name
7 abc
96 pqr
129 xyz
table1 ids save in table2s mast_id like as 7+96+129
also table2 have two coloumn id,mast_id and one record init
id mast_id
1 7+96+129

whan i retrieve the table2 row it will seprate the mast_id like 7,96 and 129.

推荐答案

尝试一下------
try this ------
declare @Rowresult varchar(80)
select @Rowresult=mastId from table2 where id=1
// @Rowresult='7+96+129'
DECLARE @csv VARCHAR(255)
   SET @csv =@Rowresult;
   with s(start) as(
    SELECT distinct charindex('+','+'+@csv+'+',p)
    FROM
   (
    select * from
   (
     select row_number() over (order by m1.number) p from master..spt_values m1,master..spt_values m2
    ) z
   where p <=len(@csv)+2) x
    ),
    chunks(chunk) as
             (
    select
    substring(@csv,start,(select min(start) from s as s2 where s2.start>s.start)-start-1)
   from s
   where start<len(@csv)+2
     )
    select * from chunks


看起来像你''将键值连接到单个列中.永远不要这样做.而是重新建模表以包含一个外键-类似主键的引用.

如何实现这一点取决于您是在谈论C#中的数据表,数据库中的表还是其他东西.但是基本的想法(如果我正确理解了这个问题)将是在表2中显示表1中的密钥.
Looks like you''re concatenating key values to a single column. This should never be done. Instead re-model your tables to contain a foreign key - primary key -like reference.

How to implement that depends if you''re talking about data tables in C#, tables in a database or something else. But the basic idea (if I understand the question correctly) would be to have the key from table 1 present in table 2.


这篇关于如何检索行.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆