查找诸如“NOT EXISTS”之类的条件

发布于 2024-08-15 06:13:08 字数 455 浏览 4 评论 0原文

我的数据库中有 2 个表...

Entita
id int(11)
描述 varchar(50)
.....

公共对象
....
model varchar(50) 我需要的模型(在本例中是'Entita'
model_id int(11)

我想进行如下查询:
选择实体。* 来自实体 where NOT EXISTS (select * from publicobjects where publicobjects.model = 'Entita' and publicobjects.model_id = entita.id)

如何在不使用自定义查询的情况下使用 Cakephp 的模型函数来执行此操作?

谢谢

I have 2 tables in my db...

Entita
id int(11)
descrizione varchar(50)
.....

Publicobjects
....
model varchar(50) the model I need (in this case 'Entita')
model_id int(11)

I would like to make a query like this:
select entita.*
from entita
where NOT EXISTS (select * from publicobjects where publicobjects.model = 'Entita' and publicobjects.model_id = entita.id)

How can I do this with the model functions of Cakephp without use custom query?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一张白纸 2024-08-22 06:13:08

我相信您正在尝试从 Entita 表中查找不在 Publicobjects 表中的行。假设这是正确的,下面是 MySQL 查找它的 SQL 查询:

SELECT `entita`.*
FROM `entita` 
LEFT JOIN `publicobjects` ON (`publicobjects`.`model` = 'entita' 
    AND `publicobjects`.`model_id` = `entita`.`id`)
WHERE `publicobjects`.`model_id` IS NULL

要使其与 CakePHP 的模型一起工作,需要执行几个步骤。我对你的型号名称做了一些假设,但我可能是错的,而且这些很容易修复。

首先将其添加到 Entita 模型中:

<?php
var $hasOne = array('Publicobject' => array(
    'foreignKey' => 'model_id',
    'conditions' => 'Publicobject.model = "Entita"'));

现在,您可以检查 Publicobjects 表中缺少的条目,如下所示:

<?php
$this->Entita->find('all', array('conditions' => array('Publicobject.model_id IS NULL')));

I believe you're trying to find rows from the Entita table that are not in the Publicobjects table. Assuming that is correct, here is the SQL query for MySQL to find it:

SELECT `entita`.*
FROM `entita` 
LEFT JOIN `publicobjects` ON (`publicobjects`.`model` = 'entita' 
    AND `publicobjects`.`model_id` = `entita`.`id`)
WHERE `publicobjects`.`model_id` IS NULL

To make this work with CakePHP's models takes a couple of steps. I've made some assumptions about your model names, but I could be wrong and those are easy to fix.

First add this to the Entita model:

<?php
var $hasOne = array('Publicobject' => array(
    'foreignKey' => 'model_id',
    'conditions' => 'Publicobject.model = "Entita"'));

Now, you can check for entries that are missing in the Publicobjects table like this:

<?php
$this->Entita->find('all', array('conditions' => array('Publicobject.model_id IS NULL')));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文