我是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列”,但我想删除一行而不是列。我已经看了几个类似问题的答案,并试图模仿答案,但我没有运气。
答案 0 :(得分:1)
您可以使用加入SQL的概念来尝试此操作。在这里,我们将使用左连接。
delete course from
course left join section on course.course_id = section.course_id
where course.course_id = NULL;
注意:这里我假设course_id在两个表中都存在,就像外键一样,如果不是,为了更清晰,你能否共享两个表的模式?
希望这有帮助
答案 1 :(得分:0)
试试这个:
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));
答案 2 :(得分:0)
您的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
。
答案 3 :(得分:0)
DELETE
FROM course
WHERE course_id="BIO-399" AND title = "Computational Biology" AND credits ="4";
答案 4 :(得分:0)
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)