现在需要将三张表同主表一起查询出来该怎么写sql
UNION ALL
这个好复杂 我子表每增加一个都要UNION ALL一次。。。
3 个解决方案
#1
SELECT * FROM
(
SELECT '跳转' AS doaction,'印章归还' AS `money`,'' AS `settingid`,
msgid,msgcomment,paynotify.billno,
paynotify.`payenddate` AS `paytime`,
paynotify.`paybegindate`,
seal.docname,
(SELECT elyname FROM employee WHERE employee.`ElyId` = paynotify.`createelyid`) AS createelyname,
sysfunction.*
FROM paynotify
INNER JOIN sealmovebill seal ON seal.`billno` = paynotify.`billno`
INNER JOIN wftype ON seal.`wftypeid` = wftype.`wftypeid`
INNER JOIN sysfunction ON wftype.`funcid` = sysfunction.`FuncId`
) A
UNION ALL
SELECT * FROM
(
SELECT '跳转' AS doaction,paysetting.`paymoney` AS money,paysetting.`settingid`,
msgid,msgcomment,paynotify.billno,
paysetting.`paytime`,
paynotify.`paybegindate`,
days.docname,
(SELECT elyname FROM employee WHERE employee.`ElyId` = paynotify.`createelyid`) AS createelyname,
sysfunction.*
FROM paynotify
INNER JOIN paysetting ON paysetting.`settingid` = paynotify.`settingid`
INNER JOIN dayagreementexecbill days ON days.`billno` = paynotify.`billno`
INNER JOIN wftype ON days.`wftypeid` = wftype.`wftypeid`
INNER JOIN sysfunction ON wftype.`funcid` = sysfunction.`FuncId`
)B
类似这种可能后面还有其他子表。这个sql语句怎么优化?
(
SELECT '跳转' AS doaction,'印章归还' AS `money`,'' AS `settingid`,
msgid,msgcomment,paynotify.billno,
paynotify.`payenddate` AS `paytime`,
paynotify.`paybegindate`,
seal.docname,
(SELECT elyname FROM employee WHERE employee.`ElyId` = paynotify.`createelyid`) AS createelyname,
sysfunction.*
FROM paynotify
INNER JOIN sealmovebill seal ON seal.`billno` = paynotify.`billno`
INNER JOIN wftype ON seal.`wftypeid` = wftype.`wftypeid`
INNER JOIN sysfunction ON wftype.`funcid` = sysfunction.`FuncId`
) A
UNION ALL
SELECT * FROM
(
SELECT '跳转' AS doaction,paysetting.`paymoney` AS money,paysetting.`settingid`,
msgid,msgcomment,paynotify.billno,
paysetting.`paytime`,
paynotify.`paybegindate`,
days.docname,
(SELECT elyname FROM employee WHERE employee.`ElyId` = paynotify.`createelyid`) AS createelyname,
sysfunction.*
FROM paynotify
INNER JOIN paysetting ON paysetting.`settingid` = paynotify.`settingid`
INNER JOIN dayagreementexecbill days ON days.`billno` = paynotify.`billno`
INNER JOIN wftype ON days.`wftypeid` = wftype.`wftypeid`
INNER JOIN sysfunction ON wftype.`funcid` = sysfunction.`FuncId`
)B
类似这种可能后面还有其他子表。这个sql语句怎么优化?
#2
一起查询出来是多列还是多行
贴出表的结构,数据以及要求的结果
贴出表的结构,数据以及要求的结果
#3
主表
子表1
子表2
结果
出现的是多行。。。里面既有子表1与主表的联查数据又有子表2与主表的联查数据。他们查出来的列是相同的
子表1
子表2
结果
出现的是多行。。。里面既有子表1与主表的联查数据又有子表2与主表的联查数据。他们查出来的列是相同的
#1
SELECT * FROM
(
SELECT '跳转' AS doaction,'印章归还' AS `money`,'' AS `settingid`,
msgid,msgcomment,paynotify.billno,
paynotify.`payenddate` AS `paytime`,
paynotify.`paybegindate`,
seal.docname,
(SELECT elyname FROM employee WHERE employee.`ElyId` = paynotify.`createelyid`) AS createelyname,
sysfunction.*
FROM paynotify
INNER JOIN sealmovebill seal ON seal.`billno` = paynotify.`billno`
INNER JOIN wftype ON seal.`wftypeid` = wftype.`wftypeid`
INNER JOIN sysfunction ON wftype.`funcid` = sysfunction.`FuncId`
) A
UNION ALL
SELECT * FROM
(
SELECT '跳转' AS doaction,paysetting.`paymoney` AS money,paysetting.`settingid`,
msgid,msgcomment,paynotify.billno,
paysetting.`paytime`,
paynotify.`paybegindate`,
days.docname,
(SELECT elyname FROM employee WHERE employee.`ElyId` = paynotify.`createelyid`) AS createelyname,
sysfunction.*
FROM paynotify
INNER JOIN paysetting ON paysetting.`settingid` = paynotify.`settingid`
INNER JOIN dayagreementexecbill days ON days.`billno` = paynotify.`billno`
INNER JOIN wftype ON days.`wftypeid` = wftype.`wftypeid`
INNER JOIN sysfunction ON wftype.`funcid` = sysfunction.`FuncId`
)B
类似这种可能后面还有其他子表。这个sql语句怎么优化?
(
SELECT '跳转' AS doaction,'印章归还' AS `money`,'' AS `settingid`,
msgid,msgcomment,paynotify.billno,
paynotify.`payenddate` AS `paytime`,
paynotify.`paybegindate`,
seal.docname,
(SELECT elyname FROM employee WHERE employee.`ElyId` = paynotify.`createelyid`) AS createelyname,
sysfunction.*
FROM paynotify
INNER JOIN sealmovebill seal ON seal.`billno` = paynotify.`billno`
INNER JOIN wftype ON seal.`wftypeid` = wftype.`wftypeid`
INNER JOIN sysfunction ON wftype.`funcid` = sysfunction.`FuncId`
) A
UNION ALL
SELECT * FROM
(
SELECT '跳转' AS doaction,paysetting.`paymoney` AS money,paysetting.`settingid`,
msgid,msgcomment,paynotify.billno,
paysetting.`paytime`,
paynotify.`paybegindate`,
days.docname,
(SELECT elyname FROM employee WHERE employee.`ElyId` = paynotify.`createelyid`) AS createelyname,
sysfunction.*
FROM paynotify
INNER JOIN paysetting ON paysetting.`settingid` = paynotify.`settingid`
INNER JOIN dayagreementexecbill days ON days.`billno` = paynotify.`billno`
INNER JOIN wftype ON days.`wftypeid` = wftype.`wftypeid`
INNER JOIN sysfunction ON wftype.`funcid` = sysfunction.`FuncId`
)B
类似这种可能后面还有其他子表。这个sql语句怎么优化?
#2
一起查询出来是多列还是多行
贴出表的结构,数据以及要求的结果
贴出表的结构,数据以及要求的结果
#3
主表
子表1
子表2
结果
出现的是多行。。。里面既有子表1与主表的联查数据又有子表2与主表的联查数据。他们查出来的列是相同的
子表1
子表2
结果
出现的是多行。。。里面既有子表1与主表的联查数据又有子表2与主表的联查数据。他们查出来的列是相同的