需要有关复杂 SQL 查询的帮助

发布于 2024-09-13 21:20:25 字数 1450 浏览 6 评论 0原文

我有一个使用此 select 语句的存储过程:

SELECT dbo.VendorProgram.id, 
       dbo.VendorProgram.CODE, 
       dbo.Programs.ProgramName 
          + '-' + dbo.Divisions.Division 
          + '-' + dbo.Vendors.Source 
          + '-' + dbo.Mediums.Medium 
          + '-' + dbo.VendorProgram.content 
          AS SourceDetail, 
       dbo.Vendors.Source, 
       dbo.Programs.ProgramName, 
       dbo.Divisions.Division, 
       dbo.Mediums.Medium, 
       dbo.VendorProgram.content, 
       dbo.VendorProgram.url, 
       dbo.VendorProgram.cost, 
       dbo.VendorProgram.Notes, 
       dbo.VendorProgram.StartDate, 
       dbo.VendorProgram.EndDate
FROM dbo.Programs 
RIGHT OUTER JOIN dbo.VendorProgram 
                 ON dbo.Programs.id = dbo.VendorProgram.programID 
LEFT OUTER JOIN dbo.Vendors 
                 ON dbo.VendorProgram.vendorID = dbo.Vendors.mappingID 
LEFT OUTER JOIN dbo.Divisions 
                 ON dbo.VendorProgram.divisionID = dbo.Divisions.id 
LEFT OUTER JOIN dbo.Mediums 
                 ON dbo.VendorProgram.mediumID = dbo.Mediums.id

基本上,我们有一个为供应商组合代码的系统。它由从 5 个单独的表中提取的 5 个 ID 组成。这些表具有与每个 ID 匹配的实际文本,例如供应商名称、介质类型等。到目前为止,我认为这里还没有真正突破性的使用。

我需要做的是能够编写一个使用文本参数的查询来搜索这 5 个单独的表,并找到所有可以匹配的“供应商映射”。我对 SQL 世界还是个新手,所以我不太确定该查询会是什么样子。

作为我想要如何搜索的示例。我在搜索表单中输入供应商字段的文本“Face”。然后,我希望查询针对供应商表本身运行选择,以首先找到名称中带有“face”的所有可能的供应商 ID。然后,它需要从组合表中选择其中包含任何这些 ID 的所有行。

希望这是有道理的,并且是可能的。一如既往地感谢您的帮助。

I have a stored procedure that uses this select statement:

SELECT dbo.VendorProgram.id, 
       dbo.VendorProgram.CODE, 
       dbo.Programs.ProgramName 
          + '-' + dbo.Divisions.Division 
          + '-' + dbo.Vendors.Source 
          + '-' + dbo.Mediums.Medium 
          + '-' + dbo.VendorProgram.content 
          AS SourceDetail, 
       dbo.Vendors.Source, 
       dbo.Programs.ProgramName, 
       dbo.Divisions.Division, 
       dbo.Mediums.Medium, 
       dbo.VendorProgram.content, 
       dbo.VendorProgram.url, 
       dbo.VendorProgram.cost, 
       dbo.VendorProgram.Notes, 
       dbo.VendorProgram.StartDate, 
       dbo.VendorProgram.EndDate
FROM dbo.Programs 
RIGHT OUTER JOIN dbo.VendorProgram 
                 ON dbo.Programs.id = dbo.VendorProgram.programID 
LEFT OUTER JOIN dbo.Vendors 
                 ON dbo.VendorProgram.vendorID = dbo.Vendors.mappingID 
LEFT OUTER JOIN dbo.Divisions 
                 ON dbo.VendorProgram.divisionID = dbo.Divisions.id 
LEFT OUTER JOIN dbo.Mediums 
                 ON dbo.VendorProgram.mediumID = dbo.Mediums.id

Basically we have a system that put together a code for a vendor. It is made up of 5 IDs pulled from 5 separate tables. Those tables have actual text, for a vendor's name, a type of medium, etc... that match to each ID. So far nothing truly ground breaking in use here I think.

What I need to do is be able to write a query that uses paramaters, in text, to search those 5 separate tables, and find all the "vendor mappings" that could match. I am still new in the SQL world so I am not quite sure what that query would look like.

As an example of how I want to search. I enter into my search form the text "Face" for the vendor field. I would then expect the query runs a select against the vendor table itself to first find all possible vendor IDs with "face" in the name. Then it would need to select all rows from the combined table that have any of those IDs in them.

Hopefully this makes sense, and is possible. As always thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不及他 2024-09-20 21:20:25
SELECT vp.id, vp.CODE,  
    p.ProgramName + '-' + d.Division + '-' + v.Source + '-' + m.Medium + '-' + vp.content AS SourceDetail, 
    v.Source, 
    p.ProgramName, 
    d.Division, 
    m.Medium, 
    vp.content,  
    vp.url, 
    vp.cost,
    vp.Notes, 
    vp.StartDate, 
    vp.EndDate 
FROM  dbo.Vendors v
LEFT OUTER JOIN  dbo.VendorProgram vp vp.vendorID = v.mappingID 
LEFT OUTER JOIN dbo.Programs p  ON p.id = vp.programID 
LEFT OUTER JOIN  dbo.Divisions d ON vp.divisionID = d.id 
LEFT OUTER JOIN  dbo.Mediums m  ON vp.mediumID = m.id 
WHERE v.Name LIKE '%Face%'
SELECT vp.id, vp.CODE,  
    p.ProgramName + '-' + d.Division + '-' + v.Source + '-' + m.Medium + '-' + vp.content AS SourceDetail, 
    v.Source, 
    p.ProgramName, 
    d.Division, 
    m.Medium, 
    vp.content,  
    vp.url, 
    vp.cost,
    vp.Notes, 
    vp.StartDate, 
    vp.EndDate 
FROM  dbo.Vendors v
LEFT OUTER JOIN  dbo.VendorProgram vp vp.vendorID = v.mappingID 
LEFT OUTER JOIN dbo.Programs p  ON p.id = vp.programID 
LEFT OUTER JOIN  dbo.Divisions d ON vp.divisionID = d.id 
LEFT OUTER JOIN  dbo.Mediums m  ON vp.mediumID = m.id 
WHERE v.Name LIKE '%Face%'
岁月苍老的讽刺 2024-09-20 21:20:25

添加

WHERE dbo.Vendors.ID in 
 (select ID From dbo.Vendors where dbo.Vendors.Field LIKE '%'+ @QueryText + '%'

add

WHERE dbo.Vendors.ID in 
 (select ID From dbo.Vendors where dbo.Vendors.Field LIKE '%'+ @QueryText + '%'
﹎☆浅夏丿初晴 2024-09-20 21:20:25

如果您只需要与搜索相关的行,请切换到内部联接(或包含一些不为空的行),并在与供应商的联接中包括:

AND Vendor.VendorName LIKE @PassedInParam

@PassedInParam 将传递到您的过程中,并且将是带有 ' 的搜索字符串两侧的%',即'%face%'

If you want only the rows related to your search, switch to inner joins (or include some where not nulls) and in your join to Vendors, include:

AND Vendor.VendorName LIKE @PassedInParam

Where @PassedInParam would be passed into your proc and would be your search string with a '%' on either side, ie '%face%'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文