Found 4378 Articles for MySQL

MySQL query to find sum of fields with same column value?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

605 Views

Use GROUP BY clause for this. Let us first create a table −mysql> create table sumOfFieldsDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> ClientSerialNumber varchar(100),    -> ClientCost int    -> ); Query OK, 0 rows affected (0.50 sec)Following is the query to insert some records in the table using insert command −mysql> insert into sumOfFieldsDemo(ClientSerialNumber, ClientCost) values('1111', 450); Query OK, 1 row affected (0.16 sec) mysql> insert into sumOfFieldsDemo(ClientSerialNumber, ClientCost) values('2222', 550); Query OK, 1 row affected (0.15 sec) mysql> insert into sumOfFieldsDemo(ClientSerialNumber, ClientCost) values('3333', 150); Query OK, 1 row affected (0.64 ... Read More

How to find if a column is auto_increment in MySQL?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

1K+ Views

To find if a column is auto_increment in MySQL, you can use the following syntax −select COLUMN_NAME from information_schema.columns where TABLE_SCHEMA='yourDatabaseName' and TABLE_NAME='yourTableName' and EXTRA like '%auto_increment%';Let us first create a table. Here, ClientId is set AUTO_INCREMENT −mysql> create table autoIncrementTableDemo    -> (    -> ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> ClientName varchar(20),    -> ClientAge int,    -> ClientAddress varchar(100),    -> ClientCountryName varchar(100)    -> ); Query OK, 0 rows affected (0.61 sec)Now, let us find whether any of the column is auto_increment −mysql> select COLUMN_NAME from information_schema.columns where TABLE_SCHEMA='test' and TABLE_NAME='autoIncrementTableDemo' and EXTRA ... Read More

How to delete all rows except some in MySQL?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

5K+ Views

You can use NOT IN operator for the rows you do not want to delete. Following is the syntax −delete from yourTableName where yourColumnName NOT IN(‘yourValue1’, ‘yourValue2’, ‘yourValue3’, .........N);Let us first create a table −mysql> create table deleteAllRowsWithCondition    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.84 sec)Following is the query to insert some records in the table using insert command −mysql> insert into deleteAllRowsWithCondition(Name) values('Larry'); Query OK, 1 row affected (0.14 sec) mysql> insert into deleteAllRowsWithCondition(Name) values('John'); Query OK, 1 row affected ... Read More

Remove Trailing Zero in MySQL?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

1K+ Views

Use trim() function to remove trailing zeroz in MySQL. Following is the syntax −select trim(yourColumnName)+0 As anyAliasName from yourTableName;Let us first create a table −mysql> create table removeTrailingZero    -> (    -> Number DECIMAL(10, 4)    -> ); Query OK, 0 rows affected (0.83 sec)Following is the query to insert some records in the table using insert command −mysql> insert into removeTrailingZero values(10.789); Query OK, 1 row affected (0.19 sec) mysql> insert into removeTrailingZero values(89.90); Query OK, 1 row affected (0.18 sec) mysql> insert into removeTrailingZero values(8999.70); Query OK, 1 row affected (0.20 sec) mysql> insert ... Read More

How to arrange data in s specific order in MySQL?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

87 Views

Use ORDER BY IF() to arrange data in a specific order. Following is the syntax −select *from yourTableName ORDER BY IF(yourColumnName=yourValue1 OR yourColumnName=yourValue2 OR yourColumnName=yourValue3, yourColumnName, ~yourColumnName) ASC;Let us first create a table −mysql> create table arrangeDataInSpecificOrder    -> (    -> StudentId int,    -> StudentName varchar(20)    -> ); Query OK, 0 rows affected (0.64 sec)Following is the query to insert some records in the table using insert command −mysql> insert into arrangeDataInSpecificOrder values(10, 'Larry'); Query OK, 1 row affected (0.12 sec) mysql> insert into arrangeDataInSpecificOrder values(15, 'Mike'); Query OK, 1 row affected (0.09 sec) mysql> insert into ... Read More

Select rows having more than 2 decimal places in MySQL?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

935 Views

To select rows with more than 2 decimal places, use SUBSTR() function from MySQL. Let us first create a table −mysql> create table selectRows2DecimalPlacesDemo    -> (    -> Amount varchar(100)    -> ); Query OK, 0 rows affected (0.73 sec)Following is the query to insert records in the table using insert command −mysql> insert into selectRows2DecimalPlacesDemo values('234.5678'); Query OK, 1 row affected (0.17 sec) mysql> insert into selectRows2DecimalPlacesDemo values('19.50'); Query OK, 1 row affected (0.19 sec) mysql> insert into selectRows2DecimalPlacesDemo values('23.456'); Query OK, 1 row affected (0.17 sec) mysql> insert into selectRows2DecimalPlacesDemo values('12.123'); Query OK, 1 row affected (0.14 ... Read More

Replace part of string in MySQL table column?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

380 Views

To replace part of string in MySQL table column, you can use REPLACE(). Following is the syntax −update yourTableName set yourColumnName = REPLACE(yourColumnName ,'yourOldValue', 'yourNewValue');Let us first create a table −mysql> create table replacePartOfStringDemo    -> (    -> WebsiteURL varchar(100)    -> ); Query OK, 0 rows affected (0.47 sec)Following is the query to insert records in the table using insert command −mysql> insert into replacePartOfStringDemo(WebsiteURL) values('www.mysqlQuestion.com'); Query OK, 1 row affected (0.14 sec)Following is the query to display all records from the table using select statement −mysql> select * from replacePartOfStringDemo;This will produce the following output −+-----------------------+ | ... Read More

How to strip all spaces from a column in MySQL?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

1K+ Views

To strip all spaces from a column in MySQL, you can use REPLACE() function. Following is the syntax −update yourTableName set yourColumnName=REPLACE(yourColumnName, ' ', '' );Let us first create a table −mysql> create table stripAllSpacesDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(100)    -> ); Query OK, 0 rows affected (0.56 sec)Following is the query to insert records in the table using insert command −mysql> insert into stripAllSpacesDemo(Name) values('Jo h n'); Query OK, 1 row affected (0.19 sec) mysql> insert into stripAllSpacesDemo(Name) values(' Joh n'); Query OK, 1 row affected (0.16 ... Read More

How do I insert a NULL value in MySQL?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

3K+ Views

To insert a NULL value, you can use UPDATE command. Following is the syntax −UPDATE yourTableName SET yourColumnName=NULL;Let us first create a table −mysql> create table insertNullValue    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> ClientName varchar(100),    -> ClientCountryName varchar(20)    -> ); Query OK, 0 rows affected (0.54 sec)Following is the query to insert some records in the table using insert command −mysql> insert into insertNullValue(ClientName, ClientCountryName) values('Larry', 'US'); Query OK, 1 row affected (0.19 sec) mysql> insert into insertNullValue(ClientName, ClientCountryName) values('David', 'AUS'); Query OK, 1 row affected (0.09 sec) ... Read More

Check privileges (grants) for a specific user in MySQL?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

472 Views

If you want to check privileges for a specific user, then use the below syntax −SHOW GRANTS FOR 'yourUserName'@'yourHostName';The above syntax will check privileges for a specific user.To check the privileges for a specific user, then use FOR. Let’s say we have a username ‘JOHN‘ and host is ‘%’. Following is the query to get the privileges for user “JOHN” −mysql> SHOW GRANTS FOR 'JOHN'@'%';This will produce the following output −+--------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------- --------------+ | Grants for JOHN@% | +--------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------- --------------+ | GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, ... Read More

Advertisements