gpt4 book ai didi

MYSQL从表1中获取表2中不存在的记录

转载 作者:行者123 更新时间:2023-11-29 23:37:57 26 4
gpt4 key购买 nike

我创建了一个 php 函数来从 sql 表 subscriptions 中获取记录,并且我想向 mysql_query 添加一个条件,以忽略表 subscriptions 中存在的记录表removed_items,这是我的代码;

function subscriptions_func($user_id, $limit){

$subs = array();

$sub_query = mysql_query("
SELECT `subscriptions`.`fo_id`, `subscriptions`.`for_id`, `picture`.`since`, `picture`.`user_id`, `picture`.`pic_id`
FROM `subscriptions`
LEFT JOIN `picture`
ON `subscriptions`.`fo_id` = `picture`.`user_id`
WHERE `subscriptions`.`for_id` = $user_id
AND `picture`.`since` > `subscriptions`.`timmp`
GROUP BY `subscriptions`.`fo_id`
ORDER BY MAX(`picture`.`since_id`) DESC
$limit
");
while ($sub_row = mysql_fetch_assoc($sub_query)) {
$subs [] = array(
'fo_id' => $sub_row['fo_id'],
'for_id' => $sub_row['for_id'],
'user_id' => $sub_row['user_id'],
'pic_id' => $sub_row['pic_id'],
'since' => $sub_row['since']
);
}
return $subs ;
}

我的解决方案是创建另一个函数来从表removed_items中获取记录并设置一个php条件,我在其中调用subscriptions_func()来跳过/取消设置与subscriptions_func()中的记录类似的记录,如下

$sub = subscriptions_func($user_id);
foreach($sub as $sub){
$rmv_sub = rmv_items_func($sub[‘pic_id’]);

If($rmv_sub[‘pic_id’] != $sub[‘pic_id’]){
echo $sub[‘pic_id’];
}
}

该解决方案成功地跳过了表removed_items中的项目,但是该解决方案在存储在变量 $sub 中的数组中产生了间隙,从而在回显的项目中产生了木板 Blob 。

我是否可以向函数 subscriptions_func() 添加一个条件来删除所有附加条件和检查?

最佳答案

假设 idsubscriptions 的主键,subs_idremoved_items 中的外键,那么您只需要在 WHERE 子句中添加一个条件即可。像这样的东西应该有效:

...
AND `subscriptions`.id NOT IN (SELECT `removed_items`.subs_id FROM `removed_items`)
...

与您的问题无关:

关于MYSQL从表1中获取表2中不存在的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26317551/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com