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

带有两个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.

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

报告相同问题?

悬赏问题

  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥15 pyqt信号槽连接写法
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。