简体   繁体   English

如何在 MySQL 数据库中的 select 子查询中 select 多列?

[英]How to select multiple columns in a select sub-query in MySQL database?

I am trying to get a row of another table as a sub-query of the SELECT query.我正在尝试获取另一个表的一行作为 SELECT 查询的子查询。

SELECT g.ip, g.version, t.testid, t.status, m.rundate, t.runid , (select * from B where runid=t.runid and testid=t.testid) 
  FROM Tests t, Master m, Env g
        WHERE t.runid=m.runid
        AND t.runid=g.runid
        AND g.version = "1.2.3"
        AND t.testid like "%test_name%" 
        AND g.ip in ("198.18.111.222")
        order by m.rundate desc;

How can this be achieved?如何做到这一点? Thanks!谢谢!

Try with this:试试这个:

SELECT g.ip, g.version, t.testid, t.status, m.rundate, t.runid , c.* from B c 
Join Tests t on t.runid =t.testid 
join Master m on t.runid=m.runid 
join Env g on t.runid=g.runid 
WHERE g.version = "1.2.3" AND t.testid like "%test_name%" 
AND g.ip in ("198.18.111.222")
order by m.rundate desc;

at line 2 if there is any id in B table which matches with the id in Tests replace t.runid = t.testid (suppose if you have runid in B table which relates to test table testid then replace with c.runid = t.testid)to the requirement in which the B table and tests table are satisfying the scenario of matching ID's(PK and FK).在第 2 行,如果 B 表中有任何 id 与 Tests 中的 id 匹配,请替换 t.runid = t.testid(假设如果 B 表中有与测试表 testid 相关的 runid,则替换为 c.runid = t。 testid)到B表和tests表满足匹配ID(PK和FK)场景的需求。

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

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