如何在 Yii2 中使用不等于查询
How to use not equal query in Yii2
I have two tables ad_pool and advertisment. ad_pool has some data while advertisment is empty. I am using this code to select values from the first table by not equal query like this andWhere(['<>','fisttablekey','second_tbl_key']). This is the complete code i am using to retrieve the data and i also uploaded the image.
$pool1 = (new Query())>select('p.id,p.cleaner_user_id,p.ad_place_id')
->from('ad_pool p')
->innerJoin('advertisment a' , 'p.id = a.pool_id')
->where(['=','ad_place_id',1])
->andWhere(['<>','p.id','a.pool_id'])
->orderBy(new Expression('rand()'))
// ->limit(1)
->all();
var_dump($pool1);
exit();
这个 return 我空数组。需要您的帮助。提前致谢。
INNER JOIN 关键字选择在两个表中具有匹配值的记录。
由于您的广告是空的,因此不会 return 任何数据。您可以改用 LEFT JOIN。
$pool1 = (new Query())>select('p.id,p.cleaner_user_id,p.ad_place_id')
->from('ad_pool p')
->leftJoin('advertisment a' , 'p.id = a.pool_id')
->where(['=','ad_place_id',1])
->andWhere(['<>','p.id','a.pool_id'])
->orderBy(new Expression('rand()'))
->all();
这样经过3-4个小时的努力,我解决了这个问题。如果有的话,从第二个 table 中获取所需的值,然后将其转换为单个数组并在 NOT IN 条件下使用它。 (我们必须有帮助他人的热情,我们应该互相帮助)。
I have two tables ad_pool and advertisment. ad_pool has some data while advertisment is empty. I am using this code to select values from the first table by not equal query like this andWhere(['<>','fisttablekey','second_tbl_key']). This is the complete code i am using to retrieve the data and i also uploaded the image.
$pool1 = (new Query())>select('p.id,p.cleaner_user_id,p.ad_place_id')
->from('ad_pool p')
->innerJoin('advertisment a' , 'p.id = a.pool_id')
->where(['=','ad_place_id',1])
->andWhere(['<>','p.id','a.pool_id'])
->orderBy(new Expression('rand()'))
// ->limit(1)
->all();
var_dump($pool1);
exit();
这个 return 我空数组。需要您的帮助。提前致谢。
INNER JOIN 关键字选择在两个表中具有匹配值的记录。 由于您的广告是空的,因此不会 return 任何数据。您可以改用 LEFT JOIN。
$pool1 = (new Query())>select('p.id,p.cleaner_user_id,p.ad_place_id')
->from('ad_pool p')
->leftJoin('advertisment a' , 'p.id = a.pool_id')
->where(['=','ad_place_id',1])
->andWhere(['<>','p.id','a.pool_id'])
->orderBy(new Expression('rand()'))
->all();
这样经过3-4个小时的努力,我解决了这个问题。如果有的话,从第二个 table 中获取所需的值,然后将其转换为单个数组并在 NOT IN 条件下使用它。 (我们必须有帮助他人的热情,我们应该互相帮助)。