php - 两表比较和列出php
问题描述
有两个不同的表。我想在这两个表中的同名列中找到带有“%USD”的表达式不匹配并在屏幕上列出它们。
表1查询;
<?php
include("db.php");
$queb = $conn->query("select distinct symbol from tableA WHERE symbol LIKE '%USD'");
while ($resb = $queb->fetch_assoc()){
$symbolb = $resb['symbol'];
}
?>
表2查询;
<?php
include("db.php");
$quec = $conn->query("select distinct symbol from tableB WHERE symbol LIKE '%USD'");
while ($resc = $quec->fetch_assoc()){
$symbolc = $resc['symbol'];
}
?>
如何列出不在两个表之间的表达式?
解决方案
我可能没有完全理解你的问题。如果您想列出以 'USD' 开头的 intableA
但不在 intableB
的符号以及以not intableB
开头的符号,则以下查询应返回这些符号:USD
tableA
select distinct symbol from tableA
where symbol LIKE '%USD' and symbol not in (select distinct symbol from tableB)
union
select distinct symbol from tableB
where symbol LIKE '%USD' and symbol not in (select distinct symbol from tableA)
或者您可以改用外部联接:
select distinct symbol from tableA left join tableB on tableA.symbol = tableB.symbol
where tableA.symbol like ('%USD') and tableB.symbol is null
union
select distinct symbol from tableB left join tableA on tableB.symbol = tableA.symbol
where tableB.symbol like ('%USD') and tableA.symbol is null
在任何一种情况下,如果您在symbol
列上有索引,性能都会得到提高。
推荐阅读
- jenkins - Linux:找不到回退包文件夹“C:\Program Files\dotnet\sdk\NuGetFallbackFolder”
- javascript - Google Sheets JavaScript - TypeError:无法读取未定义的属性“getSheetName”
- acumatica - 支票和付款出现空“发票日期”错误
- python - 从具有嵌套结构的字典中删除混合数据类型的空值和空对象
- domain-driven-design - 基于AOP的业务规则分离
- sql - 为什么交替查询过滤器顺序没有给我相同的结果
- python - Docker compose 从 .env 文件中获取环境变量并传递给 docker build
- gentics-mesh - 如何指定要从 Gentics Mesh 查询的模式(或索引)(无 graphQL API)
- android - Xamarin.Forms 将文件 (pdf) 保存在本地存储中并使用默认查看器打开
- flutter - 很长一段时间后无法运行 Flutter 应用程序