如何根据另一个 table 上的参考数据过滤一个 table 上的数据?

How to filter data on one table based on reference data on another table?

我有两个 table account_agentbulk_index。每个table上的数据如下

account_agent

name         code         account         remark         scheme
avi          10           151362          first          as
babi         11           123478          new            as
avi          10           151721          new            as
avi          10           151765          new            as
sam          sas/01       925458          tin            sas

bulk_index

agentcode         account          customer_name
10                151362           Alok
22                265721           Rohan
10                151721           Akash

使用这些 table 我想要 account_agent table 的结果如下

account_agent

name         code         account         remark
avi          10           151362          first
avi          10           151721          new

我试过查询

select * from account_agent where code = '10' and account = (select account from bulk_index where code = '10')

这只会 return 在 bulk_index table:

中相关的数据
SELECT A.name, A.code, A.Account, A.remark
FROM account_agent A
INNER JOIN bulk_index B
   ON A.account = B.account AND A.code = B.agentcode

假设您的 account 列是唯一的,您只需执行一个简单的连接和一个 where 子句即可:

SELECT A.name, A.code, A.Account, A.remark
FROM account_agent A
INNER JOIN bulk_index B ON A.account = B.account 
WHERE a.scheme like '%as%' and a.code = '10' 
--remove the preceeding or succeeding % in like as needed

如果您只对 scheme 列等于 'as' 且 code 等于 10 的值感兴趣,那么您可以这样做:

SELECT A.name, A.code, A.Account, A.remark
FROM account_agent A
INNER JOIN bulk_index B ON A.account = B.account 
WHERE a.scheme = 'as' and a.code = '10'

这是 W3Schools 的 link 页面,它解释了 sql 中 like 运算符的用法。

SQL LIKE Operator

这个查询对我有用

SELECT A.name, A.code, A.Account, A.remark, A.scheme
FROM account_agent A
INNER JOIN bulk_index B ON A.account = B.account 
WHERE A.scheme = 'as' AND A.code = '10'
select name,code,account,remark
from account_agent
where exists(select bulk_index.agentcode from bulk_index where bulk_index.agentcode = account_agent.code)

此代码将显示 account_agent table code 存在于 bulk_index table 中的所有数据]代理代码