MySQL:在不同的表上加法和乘法 [英] MySQL: adding and multiplying on different tables

查看:53
本文介绍了MySQL:在不同的表上加法和乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张表,一张包含这样的数据:

I have two tables, one contains data like this:

link_id | counted
=================
1--------| 1
==================
2------- | 0
==================
3 -------| 1
===================

我想选择那些计数 = 1 的,然后用这里的 id 转到表链接(其 id 在上面的 link_id 表中),并将每个乘以相应的因子:

I want to select those that are counted = 1, and then with the ids here I want to go to the table link (whose ids are in the link_id table above), and multiply each by its corresponding factor:

id | factor
===========
1  |  0.3
============
2  |  0.1
===========
3  |  0.5
==========

因此对于上面的值,它将是:

So for the values above it would be:

在第一个表中计数 = 1,1 和 3.现在,

counted = 1 in first table, 1 and 3. Now,

1*0.3 + 3*.5 = 0.3+1.5 = 1.8

如何使用 MySQL 查询执行此操作?

How can I do this with a MySQL query?

推荐答案

SELECT SUM(first_table.link_id * second_table.factor) as ANSWER
FROM   first_table
LEFT JOIN second_table on first_table.link_id = second_table.id
WHERE  first_table.counted = 1

这篇关于MySQL:在不同的表上加法和乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆