简体   繁体   English

SQL-根据另一个表中的行数删除行

[英]SQL - Delete row based on number of rows in another table

How would I go about deleting a row from the table 'subjects' that has a primary id 'subject_id' based on the number of rows in another table named 'replies' that uses a 'subject_id' column as a reference. 我将如何基于另一个名为“ replies”的表(使用“ subject_id”列作为引用)在行中的行数从表“ subjects”中删除具有主ID“ subject_id”的行。

Example in pseudo code: If ('subject' has less than 1 reply){ delete 'subject'} 伪代码示例:If('subject'的回复少于1个){delete'subject'}

I don't know much about SQL triggers so I have no clue if I would be able to incorporate this directly in the database or if I'd have to write some PHP code to handle this... 我对SQL触发器了解不多,所以我不知道是否可以将其直接合并到数据库中,或者是否必须编写一些PHP代码来处理这个问题...

To delete any subjects that have had no replies, this query should do the trick: 要删除没有答复的任何主题,此查询应能解决问题:

DELETE s.* FROM subjects AS s
WHERE NOT EXISTS 
    (
    SELECT r.subject_id
    FROM replies AS r
    WHERE r.subject_id = s.subject_id
    );

Demo: DB Fiddle Example 演示: DB Fiddle示例

One of the MySQL gurus will need to weigh in on whether or not you can do this directly, but in PHP you could... MySQL专家之一需要权衡是否可以直接执行此操作,但是在PHP中,您可以...

$query = "SELECT subject_id FROM subjects WHERE subject='test'";
$return = mysqli_query($mysqli, $query);
$id = mysqli_fetch_assoc($return);

$query = "SELECT reply_id FROM replies WHERE subject_id='".$id[0]."'";
$return = mysqli_query($mysqli, $query);

if(mysqli_num_rows($return) < 1){
    $query = "DELETE FROM subjects WHERE subject_id='1'";
    $return = mysqli_query($mysqli, $query);
}

This example assumes the "subject" is unique. 本示例假定“主题”是唯一的。 In other words, SELECTing WHERE subject='test' will only ever return one subject_id. 换句话说,选择WHERE subject ='test'只会返回一个subject_id。 If you were doing this as a periodic cleaning, you would grab all the subject_id values (no WHERE clause) and loop through them to remove them if no replies. 如果您是定期清理,则将获取所有subject_id值(没有WHERE子句),并循环遍历它们,如果没有答复,则将其删除。

You can achieve this in one query by selecting all (unique) subject-ids from the replies table, and delete all subjects that doesn't have a reply in there. 您可以通过从replies表中选择所有(唯一)主题ID,然后删除其中没有答复的所有主题,在一个查询中实现此目的。 Using SELECT DISTINCT , you don't get the IDs more than once (if a subject has more than one reply), so you don't get unnecessary data. 使用SELECT DISTINCT ,您不会多次获得ID(如果一个主题有多个答复),因此不会获得不必要的数据。

DELETE FROM subjects 
WHERE subject_id NOT IN (SELECT DISTINCT subject_id FROM replies)

Any subject that doesn't have a reply should be deleted! 没有回复的任何主题都应删除!

So you want to delete all subjects with no replies: 因此,您想删除所有主题而没有回复:

DELETE FROM subjects WHERE subject_id NOT IN 
(SELECT subject_id FROM replies);

I think this is what you want... 我想这就是你想要的...

暂无
暂无

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

相关问题 mysql根据另一个表中的值删除行 - mysql delete row based on value from another table 根据另一个列值的顺序为表中的所有行编号 - Number all rows in table based on order of another column value 运行SQL查询并按另一个表中的行数排序 - running an SQL query and ordering by number of rows from another table 如何使用PHP查询MySQL的某些行,然后针对每一行,基于一个UID,基于值查询另一个表? - How to use PHP to query MySQL for certain rows, then for each row, based on a UID, query another table based on value? 当总行数超过一个数字时,删除表中的所有行 - Delete all rows from a table when total row count exceeds a number 有没有一种方法可以根据另一个表的另一行中保存的ID选择MYSQL中的多行? - Is there a way I can select multiple rows in MYSQL based on the IDs saved in another row of another table? 使用PHP显示MSQL。 想要将选定的行移动到另一个sql表并从现有表中删除 - Displaying MSQL with PHP. Want to move selected rows to another sql table and delete from existing table 当另一个表中没有行时,如何从一个表中删除行 - How do I delete a row from one table, when no more rows exist in another 如何基于另一个表的ID从表中获取行数 - How to get number of rows from table based on ID from another table 加入1个表中具有单个ID /行但在另一个表中链接到多个行(相同ID)的SQL查询? - Join SQL query that has single id/row in 1 table, but links to multiple rows(same id) in another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM