简体   繁体   English

从表A中选择在表B或表C的相似列中存在字符串的行

[英]Select rows from table A where string exists in a similar column in table B or table C

I have 3 different SQL tables that I am working with. 我正在使用3个不同的SQL表。 Here is what the table look like: 该表如下所示:

Master Accounts
---------------
CustomerNumber PK
CompanyName
More Columns...

Bill Tos
---------------
MasterCustomerNumber FK
CompanyName

Ship Tos
---------------
MasterCustomerNumber FK
CompanyName

I want to write a MS SQL query that returns all the columns in the Master Accounts table where a company name contains the string 'ama' in any of the three tables. 我想编写一个MS SQL查询,该查询返回“主帐户”表中的所有列,其中公司名称在三个表中的任何一个中都包含字符串“ ama”。

Here is my current SQL: 这是我当前的SQL:

SELECT DISTINCT
  MA.*
FROM MasterAccounts MA
LEFT JOIN BillTos BT
  ON MA.CustNo = BT.MasterCustNo
LEFT JOIN ShipTos ST
  ON MA.CustNo = ST.MasterCustNo
WHERE MA.CompanyName LIKE '%ama%' OR BT.CompanyName LIKE '%ama%' OR ST.CompanyName LIKE '%ama%'

My goal is to get all master accounts where a billto or a shipto companyname contains 'ama'. 我的目标是获取在开单或发货方公司名称中包含“ ama”的所有主帐户。

I read yours comments and maybe this go faster: 我阅读了您的评论,也许这样会更快:

select MA.* from
MasterAccounts MA
join 
(select CustNo  nmb from MasterAccounts where CompanyName LIKE '%ama%'
union 
select MasterCustNo nmb from BillTos  where CompanyName LIKE '%ama%'
union 
select MasterCustNo nmb from ShipTos where CompanyName LIKE '%ama%' ) num on (num.nmb = MA.CustNo) 

if you need more companies maybe you use company name in lower case... 如果您需要更多的公司,也许您使用小写的公司名称...

*if is small error in query i'm sorry - i read this fast.... *如果查询中有小错误,对不起-我读得很快。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL Select 将 A 列与 B 列存在的表不同,或者如果 B 列不存在,则基于 C 列的最新 - MySQL Select distinct column A from table where column B exists or if none with column B then latest based on column C 从表A中选择,但排除表B中存在匹配项的位置 - Select From Table A but Exclude where a match exists in table B Select 表 A 中的行,其中 ID 在表 B 上不存在或如果存在,select 仅当超过 3 个月大 - Select rows from table A where ID doesn't exists on table B or if exists, select only if is more than 3 months old 从表a中选择记录不在表b和表c中的位置 - select from table a where record is not in table b and table c 从表中选择带有标签 a 或 b 和 c 的行 - Select rows from table with tag a or b and c 从一个表中选择在另一个表中存在映射的行 - Select rows from one table where mapping exists in another table 从表A的过滤部分中选择行,其中列与表B中与ID匹配的行中的列的关系匹配 - Select rows from a filtered portion of Table A where a column matches a relationship with a column from the row in Table B that matches by ID MYSQL-选择不在表B中的表A行,除了行在表C中 - MYSQL - select TABLE A rows that are not in Table B except the rows are in TABLE C 从表中选择A列或B列中的值 - Select from table where value in column A or column B 检查列表A中的一部分字符串是否存在于另一个列表B中 - Check if part of string in a column table A exists in another column table B
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM