如何匹配 sql 中的两个 table 列
How to match two table columns in sql
我有这个代码
$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";
}
我在这里尝试匹配 table g 和 table p.... 如果 table g 中的任何列 == table 中的任何列p 无论列数如何,做某事但它总是回显“不匹配”
我放了“限制 10”以防 table 中的行数很多,它只会用“排序”命令匹配前 10 行。
拜托,我需要一些帮助。
首先,获取数组形式的数据
$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";
}
为什么不使用 JOIN
看看它是否 returns 有什么用?不确定我的语法是否正确,我不经常做 JOINs:
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'
我有这个代码
$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";
}
我在这里尝试匹配 table g 和 table p.... 如果 table g 中的任何列 == table 中的任何列p 无论列数如何,做某事但它总是回显“不匹配”
我放了“限制 10”以防 table 中的行数很多,它只会用“排序”命令匹配前 10 行。
拜托,我需要一些帮助。
首先,获取数组形式的数据
$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";
}
为什么不使用 JOIN
看看它是否 returns 有什么用?不确定我的语法是否正确,我不经常做 JOINs:
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'