使用子查询数据过滤 TableAdapter 的主 SQL 查询
这基本上是一个 SQL 问题,关于如何使用子查询中的数据来过滤主查询。我正在使用表适配器,但问题是如何在 SQL 中执行此操作而不使用联接。我正在使用表适配器和 ASP.NET,如本文所述:
http://msdn .microsoft.com/en-us/library/aa581776.aspx
我的问题是如何坚持不为我的表适配器使用联接的想法(原因在文章)。我有 2 个表:Phones 和 PhoneAssociations。 Phones 表包含诸如phoneID、cellNumber、姓名等内容。PhonesAssociations 表包含有关哪些电话(一对多)与该电话关联的信息。它包含诸如 AssociationID、phoneID、addedByID 之类的内容。我想为表适配器创建一个查询,该查询返回由单个 ID 添加的电话数据(手机、ID、名称)。
如果我不过滤数据并想要返回所有电话和关联数据,则 sql 很简单:
SELECT
phoneID,
cellNumber,
providerID,
password,
nickname,
added,
(SELECT
addedByID
FROM PhoneAssociations pa
WHERE p.phoneID = pa.addedByID) AS addedByID
FROM Phones AS p
但我似乎无法弄清楚如何在不使用联接的情况下通过addedByID 过滤该列表,以下内容不起作用
SELECT
phoneID,
cellNumber,
providerID,
password,
nickname,
added,
(SELECT
addedByID
FROM PhoneAssociations pa
WHERE p.phoneID = pa.addedByID) AS addedByID
FROM Phones AS p
WHERE addedBYID=1
:也不起作用:
WHERE pa.addedBYID=1
使用连接这非常简单,但我没有找到使用子选择执行此操作的语法。有没有 SQL 专家可以为我指明正确的方向?如果您查看这篇文章,您会发现需要使用子选择而不是联接,以便表适配器可以创建更新、删除和插入方法。谢谢!
需要返回的记录应保留其原始电话 ID,我需要由电话 ID 12 添加的所有电话(及其电话 ID)的列表 –contractorwolf 16 分钟前
我在评论中添加了此内容,但也许我应该在问题本身中澄清:
如果我更清楚的话,phones 表中有 3 条记录,phoneID 为 4,8 和 12,PhoneAssociations 表中有 2 条记录,phoneID 为 4 8和addedByIDs of 12我需要返回的是2条记录,其中phoneIDs为4和8,因为它们都是由12添加的
Phones
4
8
12
,
PhoneAssociations (addedByID is second column)
4, 12
8, 12
我想回答的是:phoneIDs 12添加了哪些phoneIDs?谢谢
This is basically a SQL question, on how to use data from a subselect to filter your main query. I am using tableadapters but the question is how to do this in SQL without using joins. i am using tableadapters and ASP.NET as described in this article:
http://msdn.microsoft.com/en-us/library/aa581776.aspx
my problem is how to stick to the idea of NOT using joins for my tableadapters (reasons are discussed in the article). i have 2 tables: Phones and PhoneAssociations. the Phones table contains things like phoneID, cellNumber, name, etc. the PhonesAssociations table contains info about which phones (one to many) are associated with that phone. it contains things like associationID, phoneID, addedByID. i want to create a query for the tableadapter that returns the phones data (cell, ID, name) that have been added by a single ID.
if i am not filtering the data and want to return all the phones and associated data the sql is simple:
SELECT
phoneID,
cellNumber,
providerID,
password,
nickname,
added,
(SELECT
addedByID
FROM PhoneAssociations pa
WHERE p.phoneID = pa.addedByID) AS addedByID
FROM Phones AS p
but what i cant seem to figure out how to do without using joins is filter that list by the addedByID, the following doesnt work:
SELECT
phoneID,
cellNumber,
providerID,
password,
nickname,
added,
(SELECT
addedByID
FROM PhoneAssociations pa
WHERE p.phoneID = pa.addedByID) AS addedByID
FROM Phones AS p
WHERE addedBYID=1
this also doesnt work:
WHERE pa.addedBYID=1
this is pretty simple using joins, but i am not finding the syntax for doing it using subselects. any SQL gurus out there that can point me in the right direction? If you look at the article you will see the need to use subselects instead of joins so that the tableadapters can create your update, delete and insert methods. Thanks!
the records that need to be returned should retain their original phoneID, i need a list of all the phones (with their phoneID's) that have been added by the phoneID of 12 – contractorwolf 16 mins ago
i added this in the comments, but maybe i should clarify in the question itself:
if im being more clear, and there are 3 records in the phones table with phoneIDs of 4,8 and 12 and 2 records in the PhoneAssociations table with phoneIDs of 4 and 8 and addedByIDs of 12 what i need to return is 2 records that have phoneIDs of 4 and 8 because they both were addedByID by 12
Phones
4
8
12
and
PhoneAssociations (addedByID is second column)
4, 12
8, 12
what i am trying to answer is: what phoneIDs were added by phoneID 12? thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@Thomas - 我稍微修改了你的方法来为我提供我需要的数据。
我使用的最终查询(以防其他人想要解决此类问题)在这里:
它允许我传递
addedByID
并返回与其关联的电话及其正确的phoneID
。再次感谢@Thomas的回答,它完全弥补了我的差距。
该声明本身说“给我电话数据,其中它们与
PhoneAssociations
存在关系,这些记录phoneIDs
和AddedByID
为 12(或其他) @addedByID 参数需要)。希望有帮助!@Thomas - I modified your approach a little to give me the data that I needed.
The final Query I used (in case anyone else is looking to solve this sort of problem) is here:
which allows me to pass an
addedByID
and return the phones associated with it with their correctphoneID
.Thanks again @Thomas for your answer, it totally bridged the gap for me.
The statement itself says "give me the phone data where their exists a relationship to
PhoneAssociations
with these recordsphoneIDs
and theAddedByID
of 12 (or whatever the @addedByID parameter needs to be). Hope that helps!