首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mysql查询字符串组合以获取结果

Mysql查询字符串组合以获取结果
EN

Stack Overflow用户
提问于 2013-10-15 10:29:21
回答 1查看 156关注 0票数 0

我希望将这两个mysql查询字符串连接到结果列,如worker、product、Issued、received、pro_unitrs、pro_tot

第一个==

代码语言:javascript
复制
select (select wor_code from chall_rec_master where rec_id = chall_rec_pro.rec_id) as Worker, pro_code, sum(pro_qty) as receive, pro_unitrs, pro_tot from chall_rec_pro group by pro_code

第二个==

代码语言:javascript
复制
select (select worker_code from challan_master where challan_id = challan_mast_prod.challan_id) as Worker , pro_code, sum(pro_qty) as Issued from challan_mast_prod group by pro_code

列worker、product、Issued、received、pro_unitrs、pro_tot

EN

回答 1

Stack Overflow用户

发布于 2013-10-15 10:43:30

联合查询应该做到这一点-非常简单,只需确保返回的列数相同(如NULL、pro_unitrs或'‘as pro_unitrs,这样您就不会拥有其他查询拥有的信息……Mysqltutorial.org/sql-union mysql.aspx

代码语言:javascript
复制
select * from 
((
    select 
    (select wor_code from chall_rec_master where rec_id = chall_rec_pro.rec_id) as Worker, 
    pro_code, 
    sum(IFNULL(pro_qty,0)) as receive, 
    '' as Issued,
    pro_unitrs, 
    pro_tot 
    from chall_rec_pro 
    group by pro_code
) UNION (
    select (select worker_code from challan_master where challan_id = challan_mast_prod.challan_id) as Worker , 
    pro_code, 
    '' as receive,
    sum(IFNULL(pro_qty,0)) as Issued,
    '' as pro_unitrs,
    '' as pro_tot 
    from challan_mast_prod 
    group by pro_code
)) as results

--编辑--更新查询以包括接收和发出--edit2--更新查询以不求空值之和-如果MySQL的sum()试图对空值求和,则它将输出设为空,因此将ifnull()放入其中,如果变量为空,则将变量设置为零。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19372354

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档