Mysql CakePHP具有并属于许多不存在的条件
我正在尝试设置NOT EXISTS条件,如以下SQL语句:Mysql CakePHP具有并属于许多不存在的条件,mysql,sql,cakephp,Mysql,Sql,Cakephp,我正在尝试设置NOT EXISTS条件,如以下SQL语句: $conditions = Array ( [table] => products_pages [alias] => ProductsPage [type] => inner [foreignKey] => [conditions] => Array ( [0] => ProductsPage.product_id = Product.id ) ) 因此,基本上选
$conditions = Array
(
[table] => products_pages
[alias] => ProductsPage
[type] => inner
[foreignKey] =>
[conditions] => Array
(
[0] => ProductsPage.product_id = Product.id
)
)
因此,基本上选择products_pages表中不存在的任何产品
为CakePHP格式化SQL语句并在此处替换它的正确方法是什么:
[条件]=>阵列
(
[0]=>(在此处插入以上SQL的正确方式是什么?
)
非常感谢你们的帮助,我已经花了大约5个小时试图解决这个问题,但运气不好。谢谢!如果您找不到使用CakePHP的方法,您可以随时使用
query
:
在这种情况下,安全性不会受到影响,因为您没有使用任何输入
不管怎样,简单的做法是只需一步多做:
SELECT * FROM products_pages,products
WHERE NOT EXISTS (SELECT id
from products_pages
where products_pages.product_id = products.id)
如果你认为答案是有效的,那就太好了:)(我答案左边的勾号)
//selecting the products in the productcs_pages table
$productsWithPages = /* query to get them*/
//getting an array of IDs
$productsWidthPagesIds = Hash::extract($productsWithPages, '{n}.Product.id');
//doing the NOT IN to select products without pages
$productsWithoutPages= $this->Product->find('all',
array('conditions' =>
array( 'NOT' => array('Product.id' => $productsWidthPagesIds )
)
);