dongwen5019 2015-08-11 20:33
浏览 154
已采纳

带有两个WHERE子句的MySQL SELECT语句

Problem: How do I concatenate two MySQL tables using two different columns?

I have two MySQL Tables.

**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.

I would like it so that I can do a SELECT query that retrieves the following:

[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. How can I incorporate both in one SELECT command?

  • 写回答

1条回答 默认 最新

  • dsjz1119 2015-08-11 20:42
    关注

    Join DescriptionTable to ResultsTable twice, once on query_id and once on 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器