简体   繁体   English

关于列中的选择查询-MySQL

[英]Regarding select query in the column - mysql

Want to have the multiple columns returned from select query in the column of the main select query. 希望在主选择查询的列中具有从选择查询返回的多个列。 Getting mysql error. 正在获取mysql错误。 How can I do it? 我该怎么做?

SELECT test.val1, 
   (SELECT 
     cc.id, cc.nbr,cc.addr
     FROM
        (
          Select temptable.id ,temptable.nbr,temptable.addr
          from temptable
          inner join temptable2  on temptable2.id=temptable.id
        )  cc),
       test.val2,
       test.val3
 FROM test 
 innerjoin test2 on test.id=test2.id

A subquery in the select list has to return 1 field and 1 record. 选择列表中的子查询必须返回1个字段和1个记录。 If you want values from multiple fields to appear in a single column, then you can use concat() or concat_ws() mysql functions to concatenate them into a single field. 如果希望多个字段中的值显示在单个列中,则可以使用concat()或concat_ws()mysql函数将它们连接到单个字段中。 If you want to combine multiple fields and multiple rows into one field, then above the afore mentioned 2 functions you will need group_concat() . 如果要将多个字段和多个行组合为一个字段,则在上述2个函数之上,将需要group_concat()

select concat_ws(';',field1, field2) from table

However, I think in this particular case you may want to place the subquery into the from clause and reference the columns from there in the select clause. 但是,我认为在这种特殊情况下,您可能需要将子查询放入from子句中,并在select子句中从那里引用列。

Please try the below if the four tables are related. 如果四个表相关,请尝试以下方法。

select tb1.tb_id, tb1.val1, tb2.tb_id from (select test.id as tb_id, val1, val2, val3 from test inner join test2 on test.id=test2.id) as tb1 join (select temptable.id as tb_id from temptable join temptable2 on temptable.id=temptable2.id) as tb2 on tb2.tb_id=tb1.tb_id; 从tb1连接中选择tb1.tb_id,tb1.val1,tb2.tb_id(从test.id = test2.id上的测试内部连接test2选择test.id作为tb_id,val1,val2,val3)从temptable加入temptable.id = temptable2.id上的temptable2作为tb2.tb_id = tb1.tb_id上的tb2;

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

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