简体   繁体   English

mysql-将整个行从一个表复制到另一个表,并在一个查询中添加带条件的count(*)

[英]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.

相关问题 在MySQL触发器中将整个表从一个表复制到另一个表 - Copy entire row from one table to another in MYSQL trigger MYSQL:如何将整行从一个表复制到 mysql 中的另一个表,第二个表有一个额外的列? - MYSQL: How to copy an entire row from one table to another in mysql with the second table having one extra column? 如何在mysql中将整个行从一个表复制到另一个表,而第二个表具有修改后的字段 - How to copy an entire row from one table to another in mysql with the second table having a modified field MySQL:将整行从一个复制到另一个并删除原始行 - MySQL: Copy entire row from one to another and delete original MYSQL查询将值插入一行并将数据从另一个表复制到另一行 - MYSQL Query to INSERT INTO a value into one row and copy data from another table into another row Mysql查询在多个条件和列上从一个表插入到另一个表 - Mysql Query for inserting from one table to another on Multiple conditions and columns MySQL将colmn数据从一个表复制到另一行 - MySQL copy colmn data from one table to another row MySQL将具有多个NOT IN条件的行从一个表复制到另一个表 - MySQL copy row from one table to another with multiple NOT IN criteria MySQL:使用一个查询从第二个表返回行计数 - MySQL: Return row count from second table using one query MySQL查询-从一个中选择,从另一个表中计数 - MySQL query - select from one, count from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM