我有一些问题。
这是我的样本表。
create table Board (
readLevel tinyint not null,
writeLevel tinyint not null,
PRIMARY KEY (boardID) ) engine=InnoDB character set=utf8;
create table Post (
postID int not null AUTO_INCREMENT,
title char(50) not null,
content TEXT not null,
writeDate date not null,
readCount int not null,
PRIMARY KEY (postID)) engine=InnoDB character set=utf8;
create table Save_Board_Post(
boardID char(30) not null,
postID int not null,
FOREIGN KEY (boardID) REFERENCES Board(boardID) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (postID) REFERENCES Post(postID) ON DELETE CASCADE ON UPDATE CASCADE ) engine=InnoDB character set=utf8;
第一个表是非常简单的Board,第二个是Post。
第三是董事会与邮政之间的关系表。
我想在Post表中使用SELECT命令,来自'Save_Board_Post'的SELECT结果。
e.g
select postID from Save_Board_Post where boardID= 'testBoard';
在这种情况下,控制台中的输出结果是......
帖子ID
1
2
我希望将此结果用于下一个选择。
select * from Post where (SELECT Result);
如何摆放它们?
我已经用过这句话了。
select * from Post where "select postID from Save_Board_Post where boardID= 'testBoard' ";
但是控制台没有结果。
请告诉我。谢谢!
答案 0 :(得分:0)
select *
from Post where PostID IN
(select postID
from Save_Board_Post
where boardID= 'testBoard');
答案 1 :(得分:0)
您要使用的内容称为子查询
SELECT * FROM Post p
WHERE p.postID IN (
SELECT postID FROM Save_Board_Post
WHERE boardID = 'testboard');