使用复合主键基于多条记录查询结果 [英] Query results based on multiple records using compound primary key

查看:156
本文介绍了使用复合主键基于多条记录查询结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我能够获得所选(选中)的主键,来自gridview的行,现在需要从数据库中为这些选定的复合主键检索相应的记录。

如果行仅使用一个主键,则会更容易。我可以直接连接主键(逗号分隔)并在WHERE IN子句中使用它。但行使用三个主键(int,int,string)

我正在考虑使用SELECT JOIN(每个主键选择一个,然后连接所有选择),但我不知道这是否是最优化的方式来做到这一点。



处理这个问题的最佳方法是什么?



预先感谢。

Niki

[更新] - 无法回复个别评论,我可能没有足够的权限,所以在这里更新后明确。



下面是我正在做的一个例子:

数据来自一个数据库表的复合关键字:

  TABLE1 
KeyCol1 INT(主键),
KeyCol2 STRING(主键),
KeyCol3 INT(主键),
Col4十进制,
Col5 STRING,
其他列。 。 。

第1页WITH GRIDVIEW 1:

  Sel | KeyCol1 | KeyCol2 | KeyCol3 | Col4 | Col5 |其他栏。 。 。 
[] | 100 | CODE1 | 01 | 10.05 |描述1 |。 。 。
[/] | 100 | CODE1 | 02 | 5.03 |说明2 |。 。 。
[] | 100 | CODE2 | 01 | 12.45 |描述4 |。 。 。
[/] | 102 | CODE1 | 01 | 21.50 |描述1 |。 。 。
[/] | 102 | CODE2 | 01 | 9.10 |说明5 |。 。 。
[/] | 102 | CODE3 | 03 | 7.15 |说明1 |。 。 。

。 。 。
(其中Sel列为复选框,并选中[/])

然后在第1页上点击一个按钮(例如取消记录)后,我需要获取这些行,以显示到另一个页面,如下所示:



第2页WITH GRIDVIEW 2

  KeyCol1 | KeyCol2 | KeyCol3 | Col4 |其他Colums |原因$ b $ 100 | CODE1 | 02 | 5.03 |。 。 。 | _______ 
102 | CODE1 | 01 | 21.50 |。 。 。 | _______
102 | CODE2 | 01 | 9.10 |。 。 。 | _______
102 | CODE3 | 03 | 7.15 |。 。 。 | _______

(其中Reason列为文本框)

解决方案

您可以在单个查询中通过为每个三重ids(即gridview中的每个选定行)添加OR子句来完成此操作。例如:
$ b $ p

选择无论从表1 WHERE(KeyCol1 = val1-1 AND KeyCol2 = val1-2 AND KeyCol3 = val1-3)OR(KeyCol1 = val2-1 AND KeyCol2 = val2-2 AND KeyCol3 = val2-3)或..等。


What is the best way to get records from the database based on the selected primary keys?

I am able to get the primary keys of the selected (checked) rows from a gridview, and now need to retrieve the corresponding records from the database for those selected compound primary keys.

If the rows are using just one primary key, it would have been easier. I could have just concatenated the primary keys (comma delimited) and use it in a WHERE IN clause. But the rows are using three primary keys (int, int, string)

I am thinking of using an SELECT JOIN (one select for each set of primary keys, then join all selects), but I'm not sure if this is the most optimized way to do this.

What is the best way to handle this?

Thanks in advance.

Niki

[Update] - can't reply to individual comments, I might not have enough priviledges yet, so will update post here to be clear.

Here's an example of what I am doing:

Data coming from one database table with compound key:

TABLE1
KeyCol1 INT (Primary Key),
KeyCol2 STRING (Primary Key),
KeyCol3 INT (Primary Key),
Col4    Decimal,
Col5    STRING,
Other columns . . .

PAGE 1 WITH GRIDVIEW 1:

Sel |KeyCol1 |KeyCol2 |KeyCol3 |Col4  |Col5          |Other Columns . . .
[ ] |100     |CODE1   |01      |10.05 |Description 1 |. . .
[/] |100     |CODE1   |02      | 5.03 |Description 2 |. . .
[ ] |100     |CODE2   |01      |12.45 |Description 4 |. . .
[/] |102     |CODE1   |01      |21.50 |Description 1 |. . .
[/] |102     |CODE2   |01      | 9.10 |Description 5 |. . .
[/] |102     |CODE3   |03      | 7.15 |Description 1 |. . .

. . . (where Sel column is a checkbox, and [/] is checked)

Then after clicking a button (ex. "Cancel Records") on Page 1, I need to get those rows, to display to another page like this:

PAGE 2 WITH GRIDVIEW 2

KeyCol1 |KeyCol2 |KeyCol3 |Col4  |Other Colums  |Reason
100     |CODE1   |02      | 5.03 |. . .         |_______
102     |CODE1   |01      |21.50 |. . .         |_______
102     |CODE2   |01      | 9.10 |. . .         |_______
102     |CODE3   |03      | 7.15 |. . .         |_______

(where Reason column is a textbox)

解决方案

You can do this in a single query by using adding an OR clause for each trio of ids (i.e. for each selected row in your gridview). For example:

SELECT whatever FROM Table1 WHERE (KeyCol1=val1-1 AND KeyCol2=val1-2 AND KeyCol3=val1-3) OR (KeyCol1=val2-1 AND KeyCol2=val2-2 AND KeyCol3=val2-3) OR .. etc

这篇关于使用复合主键基于多条记录查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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