update inner select successful but data not update - mysql

this is my code
UPDATE `mytable`
SET `income` = (SELECT sum(`row`) FROM (SELECT * FROM `mytable`)AS mmc WHERE `view_date` = '2018-5-21')
WHERE id = 1 AND view_date = '2018-5-21'
I run this code and it successful, but my data was not update.

You can use the below SQL to update your table properly.
SQL:
update mytable m
join (select id,view_date,sum(row) as row from mytable group by id,view_date) t on (m.id = t.id and m.view_date = t.view_date)
set m.income = t.row
where m.id = 1 and m.view_date = '2018-05-23';
Example are below:
mysql> create table mytable(id int,income int, row int, view_date date);
Query OK, 0 rows affected (0.54 sec)
mysql> insert into mytable values(1,null,230,current_date);
Query OK, 1 row affected (0.12 sec)
mysql> insert into mytable values(1,null,450,current_date);
Query OK, 1 row affected (0.05 sec)
mysql> select * from mytable;
+------+--------+------+------------+
| id | income | row | view_date |
+------+--------+------+------------+
| 1 | NULL | 230 | 2018-05-23 |
| 1 | NULL | 450 | 2018-05-23 |
| 2 | NULL | 800 | 2018-05-23 |
+------+--------+------+------------+
3 rows in set (0.00 sec)
mysql> update mytable m
-> join (select id,view_date,sum(row) as row from mytable group by id,view_date) t on (m.id = t.id and m.view_date = t.view_date)
-> set m.income = t.row
-> where m.id = 1 and m.view_date = '2018-05-23';
Query OK, 2 rows affected (0.13 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> select * from mytable;
+------+--------+------+------------+
| id | income | row | view_date |
+------+--------+------+------------+
| 1 | 680 | 230 | 2018-05-23 |
| 1 | 680 | 450 | 2018-05-23 |
| 2 | NULL | 800 | 2018-05-23 |
+------+--------+------+------------+
3 rows in set (0.00 sec)

Related

trigger to update table contribution calculation using mysql

I need to calculate contribution base on project per developer
Contribution table
-------------------------------------------------------------
| id | projected | developer id | total hours | contribution|
-------------------------------------------------------------
| 1 | 1 | 1 | 25 | |
-------------------------------------------------------------
| 2 | 1 | 2 | 75 | |
-------------------------------------------------------------
| 3 | 2 | 1 | 10 | |
-------------------------------------------------------------
need to update same table with trigger after insert and update
expected result
Contribution table
-------------------------------------------------------------
| id | projected | developer id | total hours | contribution|
-------------------------------------------------------------
| 1 | 1 | 1 | 25 | 25% |
------------------------------------------------------------
| 2 | 1 | 2 | 75 | 75% |
-------------------------------------------------------------
| 3 | 2 | 1 | 10 | 100% |
-------------------------------------------------------------
calculation for the getting contribution
project 1 :
total hours = 25 + 75 = 100
contribution per developer = 25/100*100
= 25%
i need a trigger to get this result: but don't know how to get this
This is my trigger not getting error but contribution calculation not correct
CREATE TRIGGER `update_contribution` AFTER INSERT ON `tasks`
FOR EACH ROW BEGIN
IF NOT EXISTS
(SELECT p_id ,d_id
FROM contribution
WHERE
p_id = NEW.p_id
AND
d_id = NEW.d_id)
THEN
SET #old_total_dev_hours = (SELECT SUM(total_hours)
FROM contribution
WHERE p_id = NEW.p_id
GROUP BY p_id);
SET #total_hours1 = (SELECT (total_hours)
FROM contribution
WHERE d_id = NEW.d_id AND p_id = NEW.p_id
);
SET #dev_con = #total_hours1/#old_total_dev_hours*100 ;
SET #total_hours = new.hours + new.overtime;
INSERT INTO contribution
( p_id,
d_id,
hours,
overtime,
total_hours,
contribution
)
VALUES
(NEW.p_id,
NEW.d_id,
NEW.hours,
NEW.overtime,
#total_hours ,
#dev_con
);
ELSE
UPDATE contribution
SET
hours = hours + NEW.hours ,
overtime = overtime + NEW.overtime,
total_hours = hours + overtime,
contribution = #dev_con
WHERE
p_id = NEW.p_id
AND
d_id = NEW.d_id;
END IF;
END
This is my code in this code other calculation are working fine
contribution is not getting correctly!!
Here's an updated trigger that would replace the one from your previous question. It calculates
both values for the total_hours and contribution percentage.
DELIMITER |
CREATE TRIGGER update_hours AFTER INSERT ON tasks
FOR EACH ROW
BEGIN
SET #old_total_dev_hours = (SELECT SUM(hours + overtime)
FROM contribution
WHERE p_id == new.p_id && d_id == new.d_id
GROUP BY p_id,d_id);
SET #old_total_hours = (SELECT SUM(hours + overtime)
FROM contribution
WHERE p_id == new.p_id
GROUP BY p_id);
SET #total_hours = #old_total_dev_hours + new.hours + new.overtime;
SET #contrib_percent = (#old_total_dev_hours / #old_total_hours) * 100;
INSERT INTO contribution
( p_id,
d_id,
hours,
overtime,
total_hours,
contribution )
VALUES
(
NEW.p_id,
NEW.d_id,
NEW.hours,
NEW.overtime,
#total_hours,
#contrib_percent
);
END|
DELIMITER ;
Assuming tasks logs hours worked by developers on projects then 2 things can happen 1) there is no note that a developer has worked on a project in contribution 2) a developer has worked on a project and the hours he has worked need to updated in contribution. Both cases imply that all developer contributions need to be recalculated on each insert to tasks.
For example
drop table if exists t,contribution;
create table t(id int auto_increment primary key, developerid int, projectid int,hrs_normal int, hrs_overtime int);
create table contribution(id int auto_increment primary key, projectid int,
developerid int, hrs int, contribution decimal (6,3) default 0);
drop trigger if exists t;
delimiter $$
create trigger t after insert on t
for each row
begin
declare totalhours int default 0;
set totalhours = (select ifnull(sum(hrs),0) from contribution where projectid = new.projectid);
set totalhours = totalhours + ifnull(new.hrs_normal,0) + ifnull(new.hrs_overtime,0);
if not exists (select 1 from contribution where projectid = new.projectid and developerid = new.developerid) then
insert into contribution(projectid,developerid,hrs)
values
(new.projectid,new.developerid,ifnull(new.hrs_normal,0) + ifnull(new.hrs_overtime,0)
);
else
update contribution
set hrs = hrs + ifnull(new.hrs_normal,0) + ifnull(new.hrs_overtime,0)
where developerid = new.developerid and projectid = new.projectid;
end if;
update contribution
set contribution = (hrs / totalhours) * 100
where projectid = new.projectid;
end $$
delimiter ;
MariaDB [sandbox]> truncate table t;
Query OK, 0 rows affected (0.28 sec)
MariaDB [sandbox]> truncate table contribution;
Query OK, 0 rows affected (0.25 sec)
MariaDB [sandbox]> select * from t;
Empty set (0.00 sec)
MariaDB [sandbox]> select * from contribution;
Empty set (0.00 sec)
MariaDB [sandbox]>
MariaDB [sandbox]> insert into t (developerid , projectid ,hrs_normal , hrs_overtime )
-> values
-> (1,1,10,0);
Query OK, 1 row affected (0.02 sec)
MariaDB [sandbox]>
MariaDB [sandbox]> select * from t;
+----+-------------+-----------+------------+--------------+
| id | developerid | projectid | hrs_normal | hrs_overtime |
+----+-------------+-----------+------------+--------------+
| 1 | 1 | 1 | 10 | 0 |
+----+-------------+-----------+------------+--------------+
1 row in set (0.00 sec)
MariaDB [sandbox]> select * from contribution;
+----+-----------+-------------+------+--------------+
| id | projectid | developerid | hrs | contribution |
+----+-----------+-------------+------+--------------+
| 1 | 1 | 1 | 10 | 100.000 |
+----+-----------+-------------+------+--------------+
1 row in set (0.00 sec)
MariaDB [sandbox]>
MariaDB [sandbox]> insert into t (developerid , projectid ,hrs_normal , hrs_overtime )
-> values
-> (1,1,10,0),(2,1,30,10);
Query OK, 2 rows affected (0.05 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [sandbox]>
MariaDB [sandbox]> select * from t;
+----+-------------+-----------+------------+--------------+
| id | developerid | projectid | hrs_normal | hrs_overtime |
+----+-------------+-----------+------------+--------------+
| 1 | 1 | 1 | 10 | 0 |
| 2 | 1 | 1 | 10 | 0 |
| 3 | 2 | 1 | 30 | 10 |
+----+-------------+-----------+------------+--------------+
3 rows in set (0.00 sec)
MariaDB [sandbox]> select * from contribution;
+----+-----------+-------------+------+--------------+
| id | projectid | developerid | hrs | contribution |
+----+-----------+-------------+------+--------------+
| 1 | 1 | 1 | 20 | 33.333 |
| 2 | 1 | 2 | 40 | 66.667 |
+----+-----------+-------------+------+--------------+
2 rows in set (0.00 sec)
MariaDB [sandbox]>
MariaDB [sandbox]> insert into t (developerid , projectid ,hrs_normal , hrs_overtime )
-> values
-> (1,1,10,0),(2,2,30,10);
Query OK, 2 rows affected (0.05 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [sandbox]>
MariaDB [sandbox]> select * from t;
+----+-------------+-----------+------------+--------------+
| id | developerid | projectid | hrs_normal | hrs_overtime |
+----+-------------+-----------+------------+--------------+
| 1 | 1 | 1 | 10 | 0 |
| 2 | 1 | 1 | 10 | 0 |
| 3 | 2 | 1 | 30 | 10 |
| 4 | 1 | 1 | 10 | 0 |
| 5 | 2 | 2 | 30 | 10 |
+----+-------------+-----------+------------+--------------+
5 rows in set (0.00 sec)
MariaDB [sandbox]> select * from contribution;
+----+-----------+-------------+------+--------------+
| id | projectid | developerid | hrs | contribution |
+----+-----------+-------------+------+--------------+
| 1 | 1 | 1 | 30 | 42.857 |
| 2 | 1 | 2 | 40 | 57.143 |
| 3 | 2 | 2 | 40 | 100.000 |
+----+-----------+-------------+------+--------------+
3 rows in set (0.00 sec)
Note if the business rules are that a developers hours can amended in tasks you will also need and update trigger along the same lines.

MySQL update column based on previous row (same column)

I have the following data:
ID | Image
1 | 10
2 | 11
3 |
4 |
5 |
And I would like to update the missing values with the value of the row before plus one.
The final output should be:
ID | Image
1 | 10
2 | 11
3 | 12
4 | 13
5 | 14
I thought about a select during the update, but it doesn't work.
UPDATE items AS item1
SET item1.image = (SELECT image
FROM items AS item2
WHERE item2.id < item1.id
ORDER BY item2.id DESC LIMIT 1) + 1
You can use an UPDATE with a JOIN to a derived table for this:
UPDATE Items AS i1
JOIN (
SELECT ID, #n := #n + 1 AS Image
FROM Items
CROSS JOIN (SELECT #n := (SELECT MAX(Image) FROM Items)) AS v
WHERE Image IS NULL
ORDER BY ID
) AS i2 ON i1.ID = i2.ID
SET i1.Image = i2.Image;
The derived table uses variables in order to calculate the Image values of the records having NULLs.
Demo here
Try this solution using a user variable, with a complete demo as below.
SQL:
-- data
create table items(ID int, Image int);
insert into items values
(1, 10),(2, NULL),(3, NULL),(4, NULL),(5, NULL);
SELECT * FROM items;
-- SQL needed
SET #i = 0;
UPDATE items
SET Image = (IF(Image IS NULL OR Image = '',
#i:=#i+1,
#i:=Image
));
SELECT * FROM items;
Output:
mysql> SELECT * FROM items;
+------+-------+
| ID | Image |
+------+-------+
| 1 | 10 |
| 2 | NULL |
| 3 | NULL |
| 4 | NULL |
| 5 | NULL |
+------+-------+
5 rows in set (0.00 sec)
mysql>
mysql> SET #i = 0;
Query OK, 0 rows affected (0.00 sec)
mysql> UPDATE items
-> SET Image = (IF(Image IS NULL OR Image = '',
-> #i:=#i+1,
-> #i:=Image
-> ));
Query OK, 4 rows affected (0.00 sec)
Rows matched: 5 Changed: 4 Warnings: 0
mysql> SELECT * FROM items;
+------+-------+
| ID | Image |
+------+-------+
| 1 | 10 |
| 2 | 11 |
| 3 | 12 |
| 4 | 13 |
| 5 | 14 |
+------+-------+
5 rows in set (0.00 sec)
I have same problem, and I use simple Update with variable (#)
update items,(select #n := 10) v set `Image`=#n:=#n+1 order by ID asc;
I hope to be useful this query :D
Given that the tables you present are not a sample I'd do something hacky and simple like this:
Update items Set items.Image=items.id+9 WHERE items.Image is NULL;
Oh god, I smell the incoming downvotes!

List all associate's names from a referred table?

I have a table test which looks like this:
+-------+-------+
| u1_id | u2_id |
+-------+-------+
| 1 | 2 |
| 3 | 1 |
| 2 | 1 |
| 2 | 3 |
+-------+-------+
And, u1_id and u2_id are both 'foreign keys' to another table user:
+----+-------+
| id | name |
+----+-------+
| 1 | n_foo |
| 2 | n_bar |
| 3 | n_baz |
+----+-------+
Not sure how to explain this, but:
In input, I have a single user id which can be referenced in u1_id or in u2_id.
I'd like to get the associated user to it as defined in table test using a join on table user.
For user id = 1, I should get:
n_bar
n_baz
n_bar
For user id = 2, I should get:
n_foo
n_foo
n_baz
This may be a common issue but didn't find exactly how to join these two tables using:
u1_id if my input user id is in u2_id column
u2_id otherwise
I tried something like this but it doesn't seem to work:
SELECT name
FROM test
JOIN user
ON user.id = test.u1_id
WHERE test.u1_id = #guid OR
test.u2_id = #guid AND
CASE
WHEN test.u2_id = #guid
THEN test.u2_id = test.u1_id
END;
Any ideas how to achieve this? Or may be there is a better way to design these tables, I'm completely open to any suggestions.
If I correctly understood your question, I believe you need following query:
SELECT t2.`name`
FROM `t2`
INNER JOIN (
SELECT IF(#uid = 1, t1.u1_id, t1.u2_id) as `id`
FROM `t1`
) as `t1`
WHERE t2.id = t1.id and t1.id != #uid;
I tried following:
Date base create, I don't know what columns type your are using just for demo:
create table t1 (
u1_id int,
u2_id int
);
insert into t1 values
(1, 2),
(3, 1),
(2, 1),
(2, 3);
create table t2 (
id int,
name varchar(10)
);
insert into t2 values
( 1 , 'n_foo' ),
( 2 , 'n_bar' ),
( 3 , 'n_baz' );
Then Queries:
mysql> SELECT * FROM t1;
+-------+-------+
| u1_id | u2_id |
+-------+-------+
| 1 | 2 |
| 3 | 1 |
| 2 | 1 |
| 2 | 3 |
+-------+-------+
4 rows in set (0.00 sec)
mysql> SELECT * FROM t2;
+------+-------+
| id | name |
+------+-------+
| 1 | n_foo |
| 2 | n_bar |
| 3 | n_baz |
+------+-------+
3 rows in set (0.00 sec)
mysql> SET #uid = 1;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT #uid;
+------+
| #uid |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
mysql> SELECT t2.`name`
-> FROM `t2`
-> INNER JOIN (
-> SELECT IF(#uid = 1, t1.u1_id, t1.u2_id) as `id`
-> FROM `t1`
-> ) as `t1`
-> WHERE t2.id = t1.id and t1.id != #uid;
+-------+
| name |
+-------+
| n_baz |
| n_bar |
| n_bar |
+-------+
3 rows in set (0.03 sec)
mysql> SET #uid = 2;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT #uid;
+------+
| #uid |
+------+
| 2 |
+------+
1 row in set (0.00 sec)
mysql> SELECT t2.`name`
-> FROM `t2`
-> INNER JOIN (
-> SELECT IF(#uid = 1, t1.u1_id, t1.u2_id) as `id`
-> FROM `t1`
-> ) as `t1`
-> WHERE t2.id = t1.id and t1.id != #uid;
+-------+
| name |
+-------+
| n_foo |
| n_foo |
| n_baz |
+-------+
3 rows in set (0.00 sec)
Btw, you can change join conditions if it is not what you wanted. But as it give correct results...
Give it a Try!!
What about:
SELECT IF(u1.id = #guid, u2.name, u1.name) AS name
FROM test
JOIN user u1 ON u1.id = test.u1_id
JOIN user u2 ON u2.id = test.u2_id
WHERE test.u1_id=#guid OR test.u2_id=#guid;
Using #GrijeshChauhan's schema...
SELECT * FROM
(SELECT u1_id,u2_id FROM t1
UNION ALL
SELECT u2_id,u1_id FROM t1
) x
JOIN t2
ON t2.id = x.u1_id
WHERE x.u2_id = 2;
+-------+-------+------+-------+
| u1_id | u2_id | id | name |
+-------+-------+------+-------+
| 1 | 2 | 1 | n_foo |
| 1 | 2 | 1 | n_foo |
| 3 | 2 | 3 | n_baz |
+-------+-------+------+-------+

How can I update values in MySQL table If other values are duplicates?

I have table with following structure :
TBL1
COL1 COL2 COL2
---- ---- ----
A B 1
B C 3
A C 11
A D 13
B D 10
How can I update col3 If values in col1 are duplicates ?
I want the values ​​in col3 be updated with the largest found.
Тhe resulting table to look like:
COL1 COL2 COL2
---- ---- ----
A B 13
B C 10
A C 13
A D 13
B D 10
Thanks in advance !!!
With an update joined with desired data. Correlated subqueries are not wellcome:
update T inner
join ( select c1, max( c3) as m from T) T2
on T.c1 = T2.c1
set T.c3 = T2.m;
Tested:
mysql> create table T ( c1 char(1), c3 int ) ;
Query OK, 0 rows affected (0.15 sec)
mysql> insert into T values ( 'A', 1),('B',3),('A',11),('A',13);
Query OK, 4 rows affected (0.02 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select * From T;
+------+------+
| c1 | c3 |
+------+------+
| A | 1 |
| B | 3 |
| A | 11 |
| A | 13 |
+------+------+
mysql> update T inner join ( select c1, max( c3) as m from T) T2
on T.c1 = T2.c1 set T.c3 = T2.m;
Query OK, 2 rows affected (0.07 sec)
Rows matched: 3 Changed: 2 Warnings: 0
mysql> select * from T;
+------+------+
| c1 | c3 |
+------+------+
| A | 13 |
| B | 3 |
| A | 13 |
| A | 13 |
+------+------+
4 rows in set (0.00 sec)
You can select the highest value from COL3 in a subquery and do an update query on your table with the value from the subquery
UPDATE TBL1 SET COL3 = (SELECT COL3 FROM TBL1 WHERE COL1 = 'A' ORDER BY COL3 DESC LIMIT 0,1) AS a WHERE COL1 = 'A'
First, let's load your sample data
mysql> drop database if exists dilyan_kn;
Query OK, 1 row affected (0.04 sec)
mysql> create database dilyan_kn;
Query OK, 1 row affected (0.00 sec)
mysql> use dilyan_kn
Database changed
mysql> create table TBL1
-> (col1 char(1),col2 char(1),col3 int);
Query OK, 0 rows affected (0.11 sec)
mysql> insert into TBL1 values
-> ( 'A' , 'B' , 1 ),
-> ( 'B' , 'C' , 3 ),
-> ( 'A' , 'C' , 11 ),
-> ( 'A' , 'D' , 13 ),
-> ( 'B' , 'D' , 10 );
Query OK, 5 rows affected (0.06 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from TBL1;
+------+------+------+
| col1 | col2 | col3 |
+------+------+------+
| A | B | 1 |
| B | C | 3 |
| A | C | 11 |
| A | D | 13 |
| B | D | 10 |
+------+------+------+
5 rows in set (0.00 sec)
mysql>
Looking at your desired output in the question, it looks like you want the highest value of col3 for any given col1.
Example
For col1 = A, you have distinct values 1, 11, and 13. 13 is the highest
For col1 = B, you have distinct values 3 and 10. 10 is the highest
You will need a subquery that finds the highest value of col3 for any given col1.
Here is that query:
SELECT col1,MAX(col3) maxcol3
FROM TBL1 GROUP BY col1;
Let's run that subquery
mysql> SELECT col1,MAX(col3) maxcol3
-> FROM TBL1 GROUP BY col1;
+------+---------+
| col1 | maxcol3 |
+------+---------+
| A | 13 |
| B | 10 |
+------+---------+
2 rows in set (0.00 sec)
mysql>
Let's use this subquery to JOIN against the whole table and update the col3 column whenever the col1 column of the subquery matches the col1 column of the table. Here is that query:
UPDATE
(
SELECT col1,MAX(col3) maxcol3
FROM TBL1 GROUP BY col1
) A
INNER JOIN TBL1 B USING (col1)
SET B.col3 = A.maxcol3;
Let's run that UPDATE JOIN query and SELECT all of TBL1
mysql> UPDATE
-> (
-> SELECT col1,MAX(col3) maxcol3
-> FROM TBL1 GROUP BY col1
-> ) A
-> INNER JOIN TBL1 B USING (col1)
-> SET B.col3 = A.maxcol3;
Query OK, 3 rows affected (0.05 sec)
Rows matched: 5 Changed: 3 Warnings: 0
mysql> select * from TBL1;
+------+------+------+
| col1 | col2 | col3 |
+------+------+------+
| A | B | 13 |
| B | C | 10 |
| A | C | 13 |
| A | D | 13 |
| B | D | 10 |
+------+------+------+
5 rows in set (0.00 sec)
mysql>
Mission Accomplished !!!
The reason why 5 rows matched but only 3 changed stems from the fact that the rows that have (col1,col3) being ('A',13) and ('B',10) already have the max values and don't need to be changed.

Selecting total value, and a subset of it

I have a table with different values for different countries, for example:
id| country | value
===================
1 | Argelia | 8
2 | USA | 10
1 | China | 12
1 | Italy | 13
I am interested in only one country and the total, but I'm having trouble coming up with a single query to do it. The result of this query for id 1 would be:
id| value_in_Italy | total
==========================
1 | 13 | 33
As you can see, I obtained the value for Italy, and the total value. What kind of query would produce rows like the above for a similar table?
Here is the query you need:
select id,SUM(IF(country='Italy',value,0)) italy_value,SUM(value) id_values
from countrydata where id = 1;
Here is your sample data:
drop database if exists luqita;
create database luqita;
use luqita
create table countrydata
(
id int not null,
country varchar(32),
value int not null
);
insert into countrydata values
(1,'Argelia', 8 ),(2,'USA' , 10 ),
(1,'China' , 12 ),(1,'Italy' , 13 );
select * from countrydata;
Here is your sample data loaded:
mysql> drop database if exists luqita;
Query OK, 1 row affected (0.03 sec)
mysql> create database luqita;
Query OK, 1 row affected (0.02 sec)
mysql> use luqita
Database changed
mysql> create table countrydata
-> (
-> id int not null,
-> country varchar(32),
-> value int not null
-> );
Query OK, 0 rows affected (0.12 sec)
mysql> insert into countrydata values
-> (1,'Argelia', 8 ),(2,'USA' , 10 ),
-> (1,'China' , 12 ),(1,'Italy' , 13 );
Query OK, 4 rows affected (0.05 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select * from countrydata;
+----+---------+-------+
| id | country | value |
+----+---------+-------+
| 1 | Argelia | 8 |
| 2 | USA | 10 |
| 1 | China | 12 |
| 1 | Italy | 13 |
+----+---------+-------+
4 rows in set (0.00 sec)
mysql>
Here is the proposed query executed:
mysql> select id,SUM(IF(country='Italy',value,0)) italy_value,SUM(value) id_values
-> from countrydata where id = 1;
+----+-------------+-----------+
| id | italy_value | id_values |
+----+-------------+-----------+
| 1 | 13 | 33 |
+----+-------------+-----------+
1 row in set (0.00 sec)
mysql>
Now, if you want to run this query against all rows at the same time, try this all-inclusive query:
select
B.id,
SUM(IF(A.country=B.country,B.value,0)) country_value,
SUM(IF(A.id=B.id,A.value,0)) id_values
from
countrydata A,
countrydata B
group by B.country;
Here is that all-inclusive query execute
mysql> select
-> B.id,
-> SUM(IF(A.country=B.country,B.value,0)) country_value,
-> SUM(IF(A.id=B.id,A.value,0)) id_values
-> from
-> countrydata A,
-> countrydata B
-> group by B.country;
+----+---------------+-----------+
| id | country_value | id_values |
+----+---------------+-----------+
| 1 | 8 | 33 |
| 1 | 12 | 33 |
| 1 | 13 | 33 |
| 2 | 10 | 10 |
+----+---------------+-----------+
4 rows in set (0.00 sec)
mysql>
Give it a Try !!!