search

Home  >  Q&A  >  body text

mysql - sql怎样删除所有值总和小于某个阈值的行?

如果一张表只有3个字段A,B,C
那么
delete from table where (A+B+C) < t; 即可

但是一张表有几千个字段(可能有些夸张,但是注意就是几千个,请针对这张表,不要提分表的方案)
怎么解决?

PHP中文网PHP中文网2785 days ago645

reply all(2)I'll reply

  • 迷茫

    迷茫2017-04-17 14:27:19

    You can do this by using a cursor in the stored procedure. For example, if you have 100 fields and want the sum of 99 columns to be less than 1000, you can exclude one of the columns (dep_name). You only need to manually exclude this column in SQL. Yes, if there are few columns that need to be calculated, you can just enumerate and add them as you said.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 14:27:19

    As long as your a b c are all numerical fields, your SQL statement will be enough

    I didn’t see the question clearly, I’m off topic,
    Please add some more

    Use

    select COLUMN_NAME from information_schema.COLUMNS where table_name = 'table_name'
    

    Get all the field names, and then use the obtained field names to assemble the delete sql you want,

    reply
    0
  • Cancelreply