[英]MySQL SELECT statement with two WHERE clauses
Problem : How do I concatenate two MySQL tables using two different columns? 问题 :如何使用两个不同的列连接两个MySQL表?
I have two MySQL Tables. 我有两个MySQL表。
**DescriptionTable**. Fields: {filename, ID}.
**ResultsTable**. Fields: {query_id, media_id}. Both fields reference the ID field in the DescriptionTable.
A "match" links a query_id to a specified media_id, and an entry is added into ResultsTable. “匹配”将query_id链接到指定的media_id,并将一个条目添加到ResultsTable。
I would like it so that I can do a SELECT query that retrieves the following: 我想要它,以便可以执行SELECT查询来检索以下内容:
[filename (query_id), filename (media_id)]
What I Have Tried : 我试过的 :
SELECT a.filename
FROM DescriptionTable a, ResultsTable b
WHERE a.id = b.query_id
... but this only gives me the query_id
's filename and not both of the filenames associated with query_id
and media_id
. ...但这只给了我query_id
的文件名,而不是与query_id
和media_id
关联的两个文件名。 How can I incorporate both in one SELECT command? 如何将两者合并到一个SELECT命令中?
Join DescriptionTable
to ResultsTable
twice, once on query_id
and once on media_id
. 加入DescriptionTable
到ResultsTable
两次,一次在query_id
,一旦上media_id
。
SELECT dq.filename as query_filename, dm.filename as media_filename
FROM ResultsTable r
INNER JOIN DescriptionTable dq ON r.query_id = dq.ID
INNER JOIN DescriptionTable dm on r.media_id = dm.ID
This should return rows with filenames for both query_id
and media_id
that correspond to the id pairs in the rows of ResultsTable
. 这应该与文件名的两个返回行query_id
和media_id
对应于各行的ID对ResultsTable
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.