1

我想从所有提到“非洲”的文档中选择标题和日期。我有两张桌子:

提到的地方表:

|  name  | in_document | 
------------------------
| Africa | 10001.xml   | 
| Africa | 10002.xml   | 

文件表:

| filename  | title | date       | 
----------------------------------
| 10001.xml | Foo   | 2013-01-01 | 
| 10002.xml | Bar   | 2013-01-02 | 

我知道我可以先查找文件名,然后查找标题和日期,但我想在一个查询中完成所有操作。例如,我可以这样做(不是真正的代码;它真的很粗糙):

SELECT in_document FROM mentioned_places WHERE name="$mentionedPlace"; 

进而

SELECT DISTINCT title, date FROM documents WHERE filename IN ($listOfFilenames); 

...但我希望能够在一个查询中完成这一切。有没有办法做到这一点?

4

4 回答 4

2

尝试

SELECT d.title, d.date 
  FROM documents d JOIN mentioned_places p
    ON d.filename = p.in_document
 WHERE p.name = 'Africa'
 GROUP BY d.title, d.date 

或者

SELECT DISTINCT d.title, d.date 
  FROM documents d JOIN mentioned_places p
    ON d.filename = p.in_document
 WHERE p.name = 'Africa'

这是SQLFiddle演示

于 2013-07-23T10:22:46.020 回答
1
SELECT DISTINCT d.title, d.date FROM documents AS d JOIN mentioned_places AS m ON m.in_document = d.filename WHERE m.name = '$mentionedPlace'
于 2013-07-23T10:23:17.607 回答
1

只需使用“加入”

SELECT mentioned_places.name, documents.title, documents.date FROM mentioned_places join documents ON mentioned_places.in_document=documents.filename;
于 2013-07-23T10:35:50.517 回答
0

尝试做这样的事情:

SELECT p.in_document, d.title, d.date 
FROM mentioned_pleaces p join documents d
ON p.in_document = d.filename 
WHERE p.name = "$mentionedPlace" AND d.filename IN ($listOfFilenames);
于 2013-07-23T10:25:12.870 回答