sql从表中删除所选行

yshpjwxd  于 2021-06-21  发布在  Mysql
关注(0)|答案(5)|浏览(281)

我是sql新手,不知道如何从表中删除行。我有这个:

select distinct course.course_id, title, dept_name, credits
from course, section
where course.course_id not in (select course_id from section);

当它运行时,我得到一行: BIO-399 "Computational Biology" Biology 4 现在,我如何从“课程”表中删除它?我无法搜索这个特定的类,因为这段代码需要能够处理任何具有不同类的表。
我试过了

delete from course where course_id in
   (select distinct course.course_id, title, dept_name, credits
   from course, section
   where course.course_id not in (select course_id from section));

但这行不通。它说“操作数应该包含1列”,但我想删除一行而不是一列。我看过几个类似问题的答案,并试图模仿答案,但我没有运气。

vcudknz3

vcudknz31#

你的 SQL 应该是这样的

DELETE FROM `course` WHERE `course_id`="BIO-399" AND `title`="Computational Biology" AND `credits` ="4";

分解:

Delete from table-name where condition

将从表中删除行 table-name 满足之后提供的条件 where 条款
条件是 column-name = value .

ncecgwcz

ncecgwcz2#

DELETE 
FROM course 
WHERE course_id="BIO-399" AND title = "Computational Biology" AND credits ="4";
qpgpyjmq

qpgpyjmq3#

MariaDB [sandbox]> drop table if exists school_course,school_section;
Query OK, 0 rows affected (0.19 sec)

MariaDB [sandbox]> create table school_course (id int, name varchar(2));
Query OK, 0 rows affected (0.23 sec)

MariaDB [sandbox]> create table school_section(id int, cid int, name varchar(2));
Query OK, 0 rows affected (0.20 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> insert into school_course values (1,'c1'),(2,'c2');
Query OK, 2 rows affected (0.02 sec)
Records: 2  Duplicates: 0  Warnings: 0

MariaDB [sandbox]> insert into school_section values (1,1,'s1');
Query OK, 1 row affected (0.05 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> select sc.*, ss.*
    -> from school_course sc
    -> left join school_section ss on ss.cid = sc.id;
+------+------+------+------+------+
| id   | name | id   | cid  | name |
+------+------+------+------+------+
|    1 | c1   |    1 |    1 | s1   |
|    2 | c2   | NULL | NULL | NULL |
+------+------+------+------+------+
2 rows in set (0.00 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> delete sc
    -> from school_course sc
    -> left join school_section ss on ss.cid = sc.id
    -> where ss.cid is null;
Query OK, 1 row affected (0.05 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> select sc.*, ss.*
    -> from school_course sc
    -> left join school_section ss on ss.cid = sc.id;
+------+------+------+------+------+
| id   | name | id   | cid  | name |
+------+------+------+------+------+
|    1 | c1   |    1 |    1 | s1   |
+------+------+------+------+------+
1 row in set (0.00 sec)
5lhxktic

5lhxktic4#

试试这个:

delete from course where course_id in
(select distinct course.course_id
from course, section
where course.course_id not in (select course_id from section));

据我所知,你不需要加入这个查询的课程和部分。
请尝试此操作,看看是否得到相同的结果:

select distinct course.course_id
from course
where course.course_id not in (select course_id from section)

如果是,则使用此查询删除:

delete from course where course_id in
(select distinct course.course_id
from (select * from course) as c
where c.course_id not in (select course_id from section));
ulmd4ohb

ulmd4ohb5#

您可以通过使用sql的join概念来尝试这一点。这里我们将使用左连接。

delete course from 
course left join section on course.course_id = section.course_id 
where course.course_id = NULL;

注意:这里我假设course\u id像外键一样出现在两个表中,如果不是,您能共享两个表的模式以更清晰吗?
希望这有帮助

相关问题