简体   繁体   English

使用while循环从另一个表获取总行时,MYSQL慢查询

[英]MYSQL slow query when getting total rows from another table using while loop

I am new to mysql and I am attempting to get all ID from another table where ID is equal to ID from table 1 and another conditions. 我是mysql新手,我试图从另一个表中获取所有ID ,其中ID等于table 1 ID和其他条件。

Table 1 表格1

+---------------+
| ID model line |
+---------------+
| 1   XX    C1  |
| 2   AA    C3  |
| 3   SS    C1  |
+---------------+

Table 2 ( So if my query is ID = 1 AND model = XX GROUP BY line the total rows from table 2 will return 3 since ID 1,4 and 5 is true. 表2(因此,如果我的查询是ID = 1 AND model = XX GROUP BY line行,则表2的总行将返回3,因为ID 1,4和5为true。

+----------------------------+
| ID InspectionID model line |
+----------------------------+
| 1   1            XX    C1  |
| 2   1            AA    C3  |
| 3   1            SS    C1  |
| 4   1            XX    C2  |
| 5   1            XX    C4  |
+----------------------------+

So far i have this query inside a PHP while loop it return what i wanted but the query is really slow. 到目前为止,我在PHP while循环内有此查询,它返回我想要的内容,但查询速度很慢。

$query = "SELECT * FROM table2 WHERE inspectionID = '$ID' AND model = '$modelID' GROUP BY line

So i think the best way is to join table1 and table2 and add a column total sadly my knowledge on mysql and joining table is limited, any suggestions is appreciated. 所以我认为最好的方法是加入table1和table2并添加一列,可悲的是,我对mysql了解和加入表的知识是有限的,任何建议都是值得的。

my target is to create below. 我的目标是在下面创建。

+---------------------+
| ID model line total |
+---------------------+
| 1   XX    C1    3   |
| 2   AA    C3    0   |
| 3   SS    C1    0   |
+---------------------+

Pretty simple join and aggregation. 非常简单的连接和聚合。 working demo (note demo includes both answers so far and results.) 工作演示 (注释演示包括到目前为止的答案和结果。)

We left join so we keep all records from table 1 and join to table 2 to get records matching from table 1. We set the limit on model on the join itself so the filtering is applied to the table2 before the join occurs. 我们离开了联接,因此我们保留了表1中的所有记录,并联接到表2中以获取与表1匹配的记录。我们对联接本身设置了模型限制,以便在联接发生之前将过滤应用于表2。 thus the count will only include model XX and all other counts for models will be zero. 因此该计数将仅包括模型XX,而模型的所有其他计数将为零。

SELECT T1.ID, T1.Model, T1.Line, count(T2.ID)
FROM table1 T1
LEFT JOIN Table2 T2
 on T1.Model = T2.Model
and T2.Model = 'XX'  
GROUP BY T1.ID, T1.Model, T1.Line
ORDER BY T1.ID

Just create an SQL View : 只需创建一个SQL视图

CREATE VIEW TABLE_1_2_VIEW AS
SELECT A.id, A.model, A.line, B.inspectionID, COUNT(B.ID) Total
FROM table_1 A LEFT JOIN table_2 B
ON A.model=B.model
GROUP BY A.id, A.model, A.line, B.inspectionID
ORDER BY A.id, A.model, A.line;

You can query the data from this view as if it were a table. 您可以从该视图中查询数据,就好像它是一个表一样。

SELECT id, model, line, IF(model='XX', Total, 0) Total 
FROM TABLE_1_2_VIEW
WHERE inspectionID=1;

Just that the data in this view would not be updatable. 只是此视图中的数据不可更新。

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

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