简体   繁体   English

一个从两个MySQL表中选择数据的语句。 显示名称而不是ID

[英]One statement to select data from two MySQL tables. Show name instead of/by ID

I have 2 tables 我有2张桌子

The table .issues looks like this: 表.issues看起来像这样:

id tracker_id project_id    
22       1       218

In the second table .trackers: 在第二个表.trackers中:

id    name    
1     Error

Is it possible to make one Select request to receive back the result with names of trackers instead of/with their ids? 是否可以发出一个Select请求,以使用跟踪器的名称而不是其ID的结果来返回结果? I need it to simplify the Java Request. 我需要它来简化Java请求。

I want to get smth like this: 我想得到这样的东西:

 id  (tracker_id) project_id tracker_name
 22  (1)           218        Error

I tried this, but I know it doesn't make much sense: 我试过了,但是我知道这没有多大意义:

Statement stmt1 = conn.createStatement();
ResultSet ticketsRows = stmt1.executeQuery("SELECT * FROM issues 
WHERE tracker_id IN (SELECT id FROM trackers)");

Join the trackers table and select the name column from that table instead the tracker_id 加入trackers表,然后从该表中选择name列,而不是tracker_id

SELECT i.id, t.name as tracker_name, i.project_id
FROM issues i
JOIN trackers t on i.tracker_id = t.id

将您的查询更改为:SELECT i.id,i.tracker_id,i.project_id,t.name AS tracker_name从发布问题开始,我在i.tracker_id = t.id上加入了跟踪器t

this would give you exact answer to your question -- 这将为您提供确切的答案-

SELECT IS.ID, IS.tracker_id  ,IS.project_id ,  TR.tracker_name
FROM issues IS , tracker TR
where 
 IS.tracker_id = TR.id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM