I only want to delete the column if the total field is 0 in MySQL.

Asked 2 years ago, Updated 2 years ago, 44 views

There are multiple columns in the table, and you want to leave only one or more columns in the table.
Also, I would like to change the name of the column, but I am struggling because I don't know the code.

Do I need to use stored procedures?I got there, but it was blocked in all directions.

environment

OS:Win10 64bit
MySQL: mysql Ver 14.14 Distrib 5.7.33, for Win32 (AMD64)
environment:command prompt

Current tbl

user, price, a, i, u, e, o, ka, ki, ku
yamada, 600, 1, 1, 1, 1, 1, 0, 0, 0
tanaka, 780, 1, 0, 0, 0, 0, 0, 0, 0
kato, 500, 0, 1, 0, 1, 1, 0, 0

Expected Action

*I would like to respond to both 2 and 3 by query statement instead of deleting them/rename them with ALTER TABLE tbl DROP/RENAME COLUMN~ when people see them.

After processing [after_tbl] image

 user, price, a, a, a, a, a, a, a, a.
yamada, 600, 1, 1, 1, 1, 1
tanaka, 780, 1, 0, 0, 0, 0
kato, 500, 0, 1, 0, 1, 1

mysql sql

2022-09-30 16:55

1 Answers

It may be possible with stored procedures, but the syntax is a little special and troublesome compared to ordinary programming languages, so if it's a one-time job, I can handle it with Excel or Google spreadsheets.

I prepared a sample.
https://docs.google.com/spreadsheets/d/1IazXHy5Up91VaqoLLoS4MOdeRqLpor53a5VaEDql_L4/edit?usp=sharing
*Only available for viewing

Export the data to CSV, etc., paste it into a spreadsheet, and manually enter the column name after renaming.
Then output the ALTER statement you want to execute with the SUM function and IF statement.

Simply copy and play the last ALTER line.


2022-09-30 16:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.