如何在sql中匹配两个表列

2admgd59  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(495)

我有这个密码

$db = \Config\Database::connect();

$query = $db->query("select * from g WHERE g_status = '0' ORDER BY g_date ASC Limit 10;");
foreach ($query->getResult() as $g) {
    $g_amount = $g->g_amount;
 }

$query2 = $db->query("select * from g WHERE p_status = '0' ORDER BY p_date ASC Limit 10;");
foreach ($query2->getResult() as $p) {
    $p_amount = $p->p_amount;
 }

if($p_amount ==  $g_amount){

echo "do something";

}else{

echo "No match";
}

这里我试图在表g和表p之间匹配。。。。如果表g中的任何一列是==表p中的任何一列,不管列数是多少,请执行某些操作,但它总是回显“不匹配”
我把“limit 10”放在表中的行数过多的情况下,它只会将前10行与“ordering”命令匹配。
我需要帮助。

wa7juj8i

wa7juj8i1#

首先,以数组的形式获取数据

$db = \Config\Database::connect();
$query = $db->query("select * from g WHERE g_status = '0' ORDER BY g_date ASC Limit 10;");
$g_results = $query->getResult('array'); 
$g_amounts = array_column($g_results,'g_amount');

$query2 = $db->query("select * from g WHERE p_status = '0' ORDER BY p_date ASC Limit 10;");
$p_results = $query2->getResult('array'); 
$p_amounts = array_column($p_results,'p_amount');

foreach(array_intersect($g_amounts,$p_amounts) as $amount){
   echo "do something";
}
w1e3prcc

w1e3prcc2#

为什么不使用 JOIN 看看有没有什么回报?不确定我的语法是否正确,我不经常执行连接: SELECT * FROM g g_table JOIN p p_table ON g_table.g_amount = p_table.p_amount WHERE g_table.status = '0' AND p_table.status = '0'

相关问题