简体   繁体   中英

mysql - copy entire row to from one table to another and add count(*) with conditions in one query

I got stuck with my problem. After looking on the web and SO i havent found a solution. You are my last hope :) Here's the problem:

I have three tables:

table_tmp

id | name | val | owner |
------------------------
5  | abc  | 100 | 3
6  | cde  | 200 | 4

table_ready

id | special_number | id_tmp | name_tmp | val_tmp | owner_tmp | 
---------------------------------------------------------------
1  |  0             | 1      | xyz      | 100     | 3         |
2  |  0             | 2      | zzz      | 100     | 4         |
3  |  1             | 3      | kkk      | 200     | 3         |
4  |  2             | 4      | uuu      | 130     | 3         |

Now i want to copy entire row with owner = 3 from table_tmp to table_ready. It's easy - i do it with:

INSERT INTO table_ready SELECT '', '', t.* FROM table_tmp t WHERE owner = 3;

But i want to have this query to count also all rows from table_ready that have owner_tmp = 3 and val_tmp = 100. So after query table_ready would look like this:

id | special_number | id_tmp | name_tmp | val_tmp | owner_tmp | 
---------------------------------------------------------------
1  |  0             | 1      | xyz      | 100     | 3         |
2  |  0             | 2      | zzz      | 100     | 4         |
3  |  1             | 3      | kkk      | 100     | 3         |
4  |  2             | 4      | uuu      | 130     | 3         |
5  |  3             | 5      | abc      | 100     | 3         |

What happened? values from table_tmp (name, val and owner) went to table_ready (name_tmp, val_tmp, owner_tmp), id was auto incremented, and special number is effect of query:

SELECT count(id) FROM table_ready WHERE owner_tmp = 3 AND val_tmp = 100.

How to join these queries in one?

FORTUNATELY after writing this post I went to shave my beard and make some tea and I found a solution. I do not need joining those queries at all. If anybody has a solution to question above feel free to write it - i'd be glad to improve my skills. Sorry for disturbing :)

I'm on a plane, so I can't test this right now, but try the following:

INSERT INTO table_ready SELECT '', IFNULL(r.count(distinct id),0) as special_number, t.*, 
FROM table_tmp t LEFT OUTER JOIN table_ready r ON t.owner = r.owner_temp 
WHERE t.owner = 3 and r.val_tmp = 100 GROUP BY r.owner_temp;

Otherwise, I think you'll have to use a subquery.

Edit: Added IFNULL, not sure if it's necessary.

Well, actually you've some error because you say this query:

SELECT count(id) FROM table_ready WHERE owner_tmp = 3 AND val_tmp = 100

returns 3 and it actually returns 1 given your data.

Anyway, this is the query I think you're looking for:

INSERT INTO table_ready
  SELECT '',
    (SELECT count(*) FROM table_ready
     WHERE owner_tmp = 3 AND val_tmp = 100),
  t.* FROM table_tmp t
WHERE owner = 3;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM