PHP CI sql 查询语法

标签 php mysql codeigniter

我需要一些有关查询的帮助,因为我似乎没有完全理解它。

第一 table 空缺:

vac_id
vac_title
vac_location
vac_description
is_deleted
status

第二个表vacancies_labels:

vac_id
Label_id

现在我想获得包含特定位置内所有空缺职位的输出,但它们也不能包含该位置的 label_id '10'。

SELECT `v`.*
FROM `vacancies` AS `v`
LEFT JOIN `vacancies_labels` as `vl` ON `v`.`vacancy_id` = `bl`.`vacancy_id`
WHERE `v`.`vac_location` = 'russia'
AND `v`.`is_deleted` != 1
AND `v`.`status` = 1
AND `vl`.`label_id` NOT IN ('10')
GROUP BY `v`.`vacancy_id`

这只会导致 vacancies_labels 表中记录不为 10 的空缺职位。但是,它会忽略 vacancies_labels 表中根本没有记录但符合位置范围的所有空缺职位。

我在这里缺少什么?

谢谢!

最佳答案

使用 LEFT JOIN,如果未找到记录,则值将返回 null。但是在你的 WHERE 子句中,你有

AND `vl`.`label_id` NOT IN ('10')

作为不在 doesn't consider nulls你必须做一些类似的事情...

AND ( `vl`.`label_id` NOT IN ('10') OR `vl`.`label_id` IS NULL)

关于PHP CI sql 查询语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57361029/

相关文章:

php - PHP 中的 MongoDB 查询

javascript - ERR_EMPTY_RESPONSE(每个打开的地方)

mysql - 如何在 MySQL 中使用别名对城市建模

php - where_in 只返回 1 行

php - 使用 codeigniter 将变量传递给 SELECT 子句

php - 从排序规则 Modern_Spanish_CI_AI(在 SQL Server 中)到 UTF-8(在 MySQL 中)

javascript - Bootstrap 模态不加载图像

MySql "view"、 "prepared statement"和 "Prepared statement needs to be re-prepared"

mysql - 在 for 循环中将数据发送到express.js

php - 自动完成多个文本框值