[英]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 table_tmp
id | name | val | owner |
------------------------
5 | abc | 100 | 3
6 | cde | 200 | 4
table_ready 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. 现在我想将所有者= 3的整行从table_tmp复制到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: 但是我想让这个查询也可以计算来自table_ready的所有具有owner_tmp = 3和val_tmp = 100的行。因此查询后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 | 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:
table_tmp(名称,val和所有者)中的值转到table_ready(name_tmp,val_tmp,owner_tmp),id自动递增,特殊数字表示查询的效果:
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. 编辑:添加了IFNULL,不确定是否必要。
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. 返回3,实际上根据您的数据返回1。
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.