简体   繁体   English

在“选择Mysql”语句中选择

[英]Select inside a Select Mysql statment

So I have 3 tables, db, db2, db3. 所以我有3个表,db,db2,db3。 I need all the rows from db, but also the total number in db2 and db3. 我需要db中的所有行,还需要db2和db3中的总数。 I would like to do it in one query and the query below works.. 我想在一个查询中做到这一点,下面的查询工作。

SELECT *, (SELECT COUNT(*) FROM db2) AS total2, (SELECT COUNT(*) FROM db3) AS total3  
FROM db

What I am wondering is, since those nested SQL queries show up in every row, do they run that many time? 我想知道的是,由于这些嵌套的SQL查询出现在每一行中,它们是否运行了那么多次? or does mysql optimize such that it runs it once, makes it a constant, and adds it to every row?? 还是mysql进行了优化,使其运行一次,使其成为常数,并将其添加到每一行? Its important to know in case the db gets to be pretty big; 重要的是要知道数据库是否很大。

Answers and references to documentation on this would be much appreciated. 对此文档的答案和参考将不胜感激。

I'm not sure whether mysql caches or optimizes the subquery in your question but you can rewrite your query using a cross join to make sure the count queries only run once 我不确定mysql是否缓存或优化了问题中的子查询,但是您可以使用交叉联接重写查询以确保计数查询仅运行一次

 
 
 
  
  select * from db cross join (select count(*) as total2 from db2) t1 cross join (select count(*) as totla3 from db3) t2
 
  

Update 更新

I checked the query plans and mysql already optimizes your original query because it recognizes that it's not a dependent subquery. 我检查了查询计划,并且mysql已经认识到它不是从属子查询,因此它已经优化了原始查询。 Note that select_type is SUBQUERY , which runs only once, as opposed to DEPENDENT SUBQUERY , which would run once per row (see Difference between Subquery and Correlated Subquery ) 请注意, select_typeSUBQUERY ,仅运行一次,而DEPENDENT SUBQUERY每行运行一次(请参阅DEPENDENT SUBQUERY 和Correlated DEPENDENT SUBQUERY 之间的区别 )。

Subquery 子查询

+----+-------------+-------+-------+---------------+-----------------------+---------+------+-------+-------------+
| id | select_type | table | type  | possible_keys | key                   | key_len | ref  | rows  | Extra       |
+----+-------------+-------+-------+---------------+-----------------------+---------+------+-------+-------------+
|  1 | PRIMARY     | t2    | ALL   | NULL          | NULL                  | NULL    | NULL |   106 | NULL        |
|  2 | SUBQUERY    | t1    | index | NULL          | id                    | 4       | NULL | 38511 | Using index |
+----+-------------+-------+-------+---------------+-----------------------+---------+------+-------+-------------+

Cross Join 交叉连接

+----+-------------+------------+--------+---------------+-----------------------+---------+------+-------+-------------+
| id | select_type | table      | type   | possible_keys | key                   | key_len | ref  | rows  | Extra       |
+----+-------------+------------+--------+---------------+-----------------------+---------+------+-------+-------------+
|  1 | PRIMARY     | <derived2> | system | NULL          | NULL                  | NULL    | NULL |     1 | NULL        |
|  1 | PRIMARY     | t2         | ALL    | NULL          | NULL                  | NULL    | NULL |   106 | NULL        |
|  2 | DERIVED     | t3         | index  | NULL          | id                    | 4       | NULL | 38511 | Using index |
+----+-------------+------------+--------+---------------+-----------------------+---------+------+-------+-------------+

So basically what you need to do is to join db2 and db3 into db. 所以基本上你需要做的是join DB2和DB3到数据库。 To do this you should first chose the things you need from db2 and db3 (I would not pick everything if it is not needed, it will just make the query longer/slower). 为此,您应该首先从db2和db3中选择所需的内容(如果不需要,我不会选择所有内容,只会使查询变长/变慢)。 Then joining db2 into db3 and db3 into db with all the information you need. 然后将db2和db3以及所需的所有信息加入db3。

Select
  db2.total_number,
  db3.total_number,
  db.*
From
  db2 
Inner Join
  db3 On db2.some_equal_key = db3.some_equal_key
Inner Join
  db On db3.some_equal_key = db.some_equal_key

Please note that joining tables requires at least one same value (for example the same primary key). 请注意,联接表至少需要一个相同的值(例如,相同的主键)。 This will be used for the join statement - in my example its some_equal_key 这将用于join语句-在我的示例中为some_equal_key

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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