我有5个相同的MySQL表,每个表都有不同的数据,但结构相同。
我需要总结他们的价值观,我这样做:
SELECT
SUM(alltables.Quantity) AS total_qty,
format(SUM(alltables.eur), 2) as total_eur,
format(SUM(alltables.eur_80), 2) as total_eur_80,
format(SUM(alltables.eur)*.12, 2) as detrazioni,
format(SUM(alltables.eur_80)-(SUM(alltables.eur)*.12), 2) as ricevere
FROM (
SELECT Quantity, eur, eur_80 FROM table_one WHERE user_id = 849 AND Trimestre_month = '6' AND Trimestre_year = '2017' UNION ALL
SELECT Quantity, eur, eur_80 FROM table_two WHERE user_id = 849 AND Trimestre_month = '6' AND Trimestre_year = '2017' UNION ALL
SELECT Quantity, eur, eur_80 FROM table_three WHERE user_id = 849 AND Trimestre_month = '6' AND Trimestre_year = '2017' UNION ALL
SELECT Quantity, eur, eur_80 FROM table_four WHERE user_id = 849 AND Trimestre_month = '6' AND Trimestre_year = '2017' UNION ALL
SELECT Quantity, eur, eur_80 FROM table_five WHERE user_id = 849 AND Trimestre_month = '6' AND Trimestre_year = '2017'
) alltables
这会计算客户必须得到的陈述,结果非常完美:
我制作了一个adjustment
表,其结构相同,如果出现一些计算错误,我可以添加一些额外的行来调整/修复他们的收入。
所以,我的问题就出现了:我必须从上面创建一个新的APART并使用相同的WHERE CLAUSE规范自行计算eur_80
字段,我必须将它加到{{1} }(SELECT语句中的计算字段)。
这样的事情:
我尝试过加入,但没有运气。
答案 0 :(得分:2)
我认为使用连接是正确的方法。在这种情况下,它将是!-- ADM SECTION -->
<div class="backimg" id="section-10">
<div class="bghover wow fadeInUp" data-wow-duration="1.5s" data-wow-delay=".5s">
<div class="grid flex16">
<div class="row paddtop5">
<h3 class="paraw wow fadeInUpBig" data-wow-duration="2s" data-wow-delay=".5s">ADMIN SECTION</h3>
<div class="admin">
<h3 data-wow-duration="2s" data-wow-delay=".5s"> Alege opțiunea </h3>
</div>
<!-- ban -->
<style>
ul.b {list-style-type: square;}</style>
<div class="ban">
<ul class="b">
<li>BAN SYSTEM</li>
<br>
<li>KICK SYSTEM</li>
<br>
<li>ADMIN JAIL SYSTEM</li>
<br>
</ul>
</div>
<div class="ban2">
<ul class="b">
<li>SEARCH SYSTEM</li>
<br>
<li>LOG SYSTEM</li>
<br>
<li>AWARD SYSTEM</li>
<br>
</ul>
</div>
<div class="ban3">
<ul class="b">
<li>MANAGAMENT SYSTEM</li>
<br>
<li>UNBAN SYSTEM</li>
<br>
<li>UNJAIL SYSTEM</li>
<br>
</ul>
</div>
(笛卡尔积),因此我们不需要任何条件。由于连接应用于两个子查询,每个子查询只有一行总和,所以仍然只产生一个结果行。
CROSS JOIN
为了让我每天都能听到父亲的声音并完全忽略了具有相同结构的多个表的原因,我建议将这些表合并为一个带有枚举列的表,以区分这些表。这样做可以大大简化上面的查询。
答案 1 :(得分:2)
您可以在子选择中添加它。因为您希望在总计中单独显示不同的和,所以我将该列和格式的添加移到了更高的级别:
SELECT
format(andadjust.total_eur, 2) as total_eur,
format(andadjust.total_eur_80, 2) as total_eur_80,
format(andadjust.detrazioni, 2) as detrazioni,
format(andadjust.ricevere, 2) as ricevere,
format(andadjust.difference, 2) as difference,
format(andadjust.ricevere + andadjust.difference, 2) as adjustment
FROM (
SELECT
SUM(alltables.Quantity) AS total_qty,
SUM(alltables.eur) as total_eur,
SUM(alltables.eur_80) as total_eur_80,
SUM(alltables.eur)*.12 as detrazioni,
SUM(alltables.eur_80)-(SUM(alltables.eur)*.12) as ricevere,
(SELECT sum(eur_80) FROM table_adjustment WHERE user_id = 849 AND Trimestre_month = '6' AND Trimestre_year = '2017') as difference
FROM (
SELECT Quantity, eur, eur_80 FROM table_one WHERE user_id = 849 AND Trimestre_month = '6' AND Trimestre_year = '2017' UNION ALL
SELECT Quantity, eur, eur_80 FROM table_two WHERE user_id = 849 AND Trimestre_month = '6' AND Trimestre_year = '2017' UNION ALL
SELECT Quantity, eur, eur_80 FROM table_three WHERE user_id = 849 AND Trimestre_month = '6' AND Trimestre_year = '2017' UNION ALL
SELECT Quantity, eur, eur_80 FROM table_four WHERE user_id = 849 AND Trimestre_month = '6' AND Trimestre_year = '2017' UNION ALL
SELECT Quantity, eur, eur_80 FROM table_five WHERE user_id = 849 AND Trimestre_month = '6' AND Trimestre_year = '2017'
) alltables
) andadjust