2013-12-14 48 views
0

我有一些問題。如何使用另一個選擇命令選擇結果? (mysql,php)

這是我的示例表。

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; 

第一張桌子很簡單,第二張是Post。

第三是董事會和郵政之間的關係表。

我想通過'Save_Board_Post'的SELECT結果在Post表中使用SELECT命令。

select postID from Save_Board_Post where boardID= 'testBoard'; 

在這種情況下,在控制檯輸出結果是...

帖子ID

,我想用這個結果爲下一次選擇。

select * from Post where (SELECT Result); 

如何把它們?

我已經使用這句話。

select * from Post where "select postID from Save_Board_Post where boardID= 'testBoard' "; 

但在控制檯中沒有結果。

請讓我知道。謝謝!

回答

0
select * 
from Post where PostID IN 
(select postID 
    from Save_Board_Post 
    where boardID= 'testBoard'); 
+0

哦,怎麼,你是非常快的!感謝您的回答!非常感謝你! – Pororoca

+0

我猜的條件句是必須在引號中,而不是在括號中。再次感謝! – Pororoca

+0

@Pororoca你總是可以選擇我的答案作爲正確的答謝我。 )。 –

0

要使用被稱爲子查詢

SELECT * FROM Post p 
WHERE p.postID IN (
    SELECT postID FROM Save_Board_Post 
    WHERE boardID = 'testboard'); 
相關問題