php - 省略/忽略用户已购买的任何记录

标签 php mysql codeigniter

我目前正在开发一个网站,除其他外,该网站允许用户通过显示或隐藏他们已购买的商品来过滤市场。这适用于基本的 AJAX 调用,该调用传递可用过滤器的当前条件,然后使用 CodeIgniter 的事件记录构建适当的查询。

我的问题是围绕查询进行思考,以便如果用户选择隐藏购买的商品,则查询会忽略/忽略任何相关记录(即,如果 user_id = 5 且隐藏购买为 true,则任何查询中不会返回 user_id = 5 拥有的场景)。

表:场景

-------------------------------------------------------------------------
| design_id | scene_id | scene_name | ... [irrelevant columns to the Q] |
|-----------|----------|------------|-----------------------------------|
|     1     |     1    |  welcome   |                                   |
|     1     |     2    |   hello    |                                   |
|     2     |     3    |    asd     |                                   |
-------------------------------------------------------------------------

设计表与此非常相似,包括对游戏、游戏类型、设计名称等的引用。

表:user_scenes

----------------------------------------------------------------------
| design_id | scene_id | user_id | ... [irrelevant columns to the Q] |
|-----------|----------|---------|-----------------------------------|
|     1     |     1    |    5    |                                   |
|     1     |     2    |    5    |                                   |
|     1     |     1    |    9    |                                   |
----------------------------------------------------------------------

查询

SELECT `designs`.`design_id`, `designs`.`design_name`, `scenes`.`scene_id`, `scenes`.`scene_name`, `scenes`.`scene_description`, `scenes`.`scene_unique_code`, `scenes`.`date_created`, `scenes`.`scene_cost`, `scenes`.`type`, `games`.`game_title`, `games`.`game_title_short`, `games_genres`.`genre` 
FROM (`scenes`) 
JOIN `designs` ON `designs`.`design_id` = `scenes`.`design_id` 
JOIN `games` ON `designs`.`game_id` = `games`.`game_id` 
JOIN `games_genres` ON `games`.`genre_id` = `games_genres`.`genre_id` 
WHERE `scenes`.`private` = 0 
ORDER BY `designs`.`design_name` asc, `scenes`.`scene_name` asc 
LIMIT 6

该查询使用 CodeIgniter 的事件记录 ($this->db->select()/$this->db->where()),但这有些无关紧要。

--

我尝试过使用 user_scenes 进行 INNER JOIN ,然后按 scene_id 进行分组,但这会带来一个问题,即仅返回 中存在的场景用户场景。然后我尝试了子查询,但随后质疑这是否是正确的路线。

我知道还有其他方法 - 循环返回的数据并查询特定用户是否存在该记录,但我怀疑效率非常低。因此,我不知道该尝试什么,并希望得到任何帮助。

最佳答案

我不知道你的设置是否允许,但我会做一个子选择:

通过NOT IN:

SELECT * FROM `scenes` 
    WHERE `scenes`.`scene_id` NOT IN (SELECT `scene_id` FROM `user_scenes` WHERE `user_id` = 5)

或者也许通过LEFT JOIN:

SELECT * FROM `scenes` 
    LEFT JOIN (SELECT `scene_id`, `user_id` FROM `user_scenes` WHERE `user_id` = 5) AS `user_scenes`
    ON `scenes`.`scene_id` = `user_scenes`.`scene_id`
WHERE `user_scenes`.`user_id` IS NULL

我想第一种方法更快。

关于php - 省略/忽略用户已购买的任何记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27214937/

相关文章:

php - 如何获得否。在 php 的记录集变量中返回的行数?

php - php中如何防止快速页面请求?

php - 如何确定 cronjob 中的环境

javascript - AJAX 重定向

mysql - 使用 Codeigniter 中两个表中的类似功能使用用户名或电子邮件进行搜索

php - in_array 无法从 fetch_array 创建的数组中工作

php - 在 php 代码中使用 "INSERT INTO"语句不起作用

mysql - SQL查询: BETWEEN

MySql Workbench 安装程序需要安装 Visual C++ 2015 Redistributable Package,但它已经安装

MySQL 返回完整的 table.column 名称