简体   繁体   中英

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

I have 2 tables

The table .issues looks like this:

id tracker_id project_id    
22       1       218

In the second table .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? I need it to simplify the Java Request.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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