簡體   English   中英

Select,從表中統計和刪除記錄簡化

[英]Select, count and delete records from table simplify

我有個問題。 我有如下 3 個表:

1) met_ID (primair) 等

2) magn_ID (primair) met_ID (secondary)...

3) sens_ID (primair) met_ID (secondary)...

我必須刪除表 1 中的記錄,其中表 2 或 3 中的 met_ID 計數小於 10..

SELECT COUNT(*) AS count, met_ID
FROM tbl_1, tbl_2
Where met_ID = met_ID
GROUP BY met_ID

我進行了查詢,並在 PHP 中使用 WHILE 循環刪除了表 1 中的記錄。它可以很好地刪除 1000 條記錄,但我不確定它是否會完全用於表中的 100 000 條或更多記錄。

有什么方法可以完成我在 sql 查詢中所做的事情嗎?

首先使用具有...的子查詢

其次,您沒有選擇 table2 和 table3 中出現次數的總和,而是在進行交叉連接,因此您的計數實際上是乘積...

試試這個

DELETE from table_1 where met_ID IN 
(
  select met_ID FROM
  (
      SELECT met_ID FROM tbl_2   
      union
      select met_id FROM tbl_3
  )
  GROUP BY met_ID
  HAVING count(*) < 10
);
DELETE from tbl_1
WHERE 10 > (select count(*) from tbl_2 where tbl2.met_id=tbl1.met_id) OR
      10 > (select count(*) from tbl_3 where tbl3.met_id=tbl1.met_id)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM