如何使用表的主键从表中获取记录,而另一个记录具有相同的ID,但名称不同? [英] How to get records from table using it's primary key where another record have same id with different Name?

查看:45
本文介绍了如何使用表的主键从表中获取记录,而另一个记录具有相同的ID,但名称不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取诸如理货之类的记录我遇到一个问题.当我搜索诸如office之类的任何记录时,我有一种带有名称帐户搜索的表格,那么我想获取所有具有office主键的记录. br/>
例如:
我有一张桌子tblAccount

字段是:
accountID主键
accountName varchar
parentid int

当我插入任何新记录时,其父ID为0.但是当我选择插入的任何数据时,新记录的父ID便成为该数据的主键.

accountID accountName parentID
1主页0
2个办公室0
3薪水2
4家具2


当我搜索办公室时,我想要以下结果:

3薪水2
4家具2

如果办公室没有记录,它将显示为:
2 office 0

How to get records like tally i have one problem.I have one form with name account search when i search any record like office then i want to get all record which have office primary key.

example:
i have one table tblAccount

Fields are:
accountID primary key
accountName varchar
parentid int

when i inserted any new record it''s parent id is 0 .But when i select any data who are inserted then new record''s parent id is became that data primary key.

accountID accountName parentID
1 Home 0
2 offcie 0
3 salary 2
4 Furniture 2


when i search office then i want following result:

3 salary 2
4 Furniture 2

if office have no record then it will display like :
2 office 0

推荐答案

我认为您是在说,如果第二个数字是主要对象的ID,则您的数据库具有层次结构.所以你想做类似的事情
I think you''re saying that if the second number is the id of the main object, so your DB has a heirarchy. So you want to do something like
if (select count(*) from tbl where parentID = @ID > 0)
begin
 select * from tbl where parentId = @Id
end
else
begin 
 select * from tbl where accountId = @id
end


在存储的过程中.


in a stored proc.


这篇关于如何使用表的主键从表中获取记录,而另一个记录具有相同的ID,但名称不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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