简体   繁体   English

MySQL:抓取在另一个表中找不到ID的行

[英]MySQL: Grab rows where an ID isn't found in another table

Hope this makes sense, bad at this. 希望这是有道理的,不好的。

Here's the problem: I am trying to get a list of posts from an "articles" table and each article has a list of tags associated with them in another table called it "article_category_reference". 问题在于:我试图从“文章”表中获取帖子列表,每篇文章都有一个与其关联的标签列表,在另一个名为“article_category_reference”的表中。

Users block certain tags to customize their feed, so I want to filter them out of course. 用户会阻止某些标记来自定义其Feed,因此我希望将其过滤掉。 These will be in an array like (12 and 19 being tag ids): 这些将在一个数组中(12和19是标记ID):

$in = [0 => 12, 1 => 19];

Here's the current sample MySQL: 这是当前的MySQL示例:

SELECT a.`article_id`,
       a.`title`,
       a.`text`
FROM   `articles` a
       LEFT JOIN `article_category_reference` r
              ON r.article_id = a.article_id
WHERE  r.category_id NOT IN ( ?,? )
ORDER  BY a.`date` DESC
LIMIT  ?, ?  

My problem is, it will filter out articles if they only have a single tag so I have it completely wrong. 我的问题是,如果他们只有一个标签,它会过滤掉文章,所以我完全错了。 I guess I can't simply join a table like that to filter? 我想我不能简单地加入这样的表来过滤?

You can perform a nested query. 您可以执行嵌套查询。 Very useful on Mysql 在Mysql上非常有用

SELECT a.`article_id`,
   a.`title`,
   a.`text`
FROM   `articles` a
   WHERE a.article_id IN ( 
         SELECT r.`article_id` 
         FROM `article_category_reference` r
         WHERE  r.category_id NOT IN ( ?,? )
);

In simple terms go to article_category_reference get the all the ids but these and use these ids to query the articles table. 简单来说,转到article_category_reference获取所有id但是这些并使用这些id来查询articles表。

You could simply construct the query using a comma separated list of the values that you want to filter out, ie: 您可以使用要过滤掉的值的逗号分隔列表来构造查询,即:

WHERE  r.category_id NOT IN ( 12, 19 )

I don't understand the part though where you say that 虽然你这么说,我不明白这个部分

it will filter out articles if they only have a single tag 如果文章只有一个标签,它会过滤掉文章

Maybe you could show a complete example with query, sample data, the result that you are getting and the result that you are expecting if this does not answer your question. 也许你可以展示一个完整的例子,包括查询,样本数据,你得到的结果,以及你没有回答你的问题所期望的结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从数据库表中删除未在元组/列表中找到ID的行 - How can I delete rows from a database table where the ID isn't found in a tuple/list 如果在mysql表中找不到项目,如何重定向到另一页? - How to redirect to another page if item isn't found in mysql table? MySQL如何返回在另一个表中找到值的行 - MySQL How to return rows where values are found in another table 如何从 MySQL 中另一个表的两个不同列中的任何一个中不存在 ID 值的表中获取所有行? - How to get all rows from a table where the ID value doesn't exist in either of two different columns in another table in MySQL? 三表MySQL LEFT JOIN不返回不涉及一个表的行 - Three table MySQL LEFT JOIN not returning rows where one table isn't involved MySQL查询会删除A表中未在B表上找到ID的所有行? - MySQL query that deletes all rows from A table where the id is not found on B table? 选择表中ID等于另一个表中另一个ID的行 - Select rows in a table where id is equals to another id in another table 从该用户未登录且不在其他表行中的表中选择一个用户 - Select a user from table where the user isn't logged in and not a part of another tables rows MySQL-如何在另一个表的逗号分隔字段中选择ID值所在的表中的行? - MySQL - How to select rows in a table where id value is in a comma delimited field in another table? Laravel查询,显示来自一个表的结果,其中其ID不在另一个表中 - Laravel query, show results from one table where their id isn't in another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM