MySQL: adding and multiplying on different tables
I have two tables, one contains data like this:
link_id | coun开发者_JAVA技巧ted
=================
1--------| 1
==================
2------- | 0
==================
3 -------| 1
===================
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:
counted = 1 in first table, 1 and 3. Now,
1*0.3 + 3*.5 = 0.3+1.5 = 1.8
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
精彩评论