Found 6702 Articles for Database

Get all MySQL records from the previous day (yesterday)?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

2K+ Views

To get the records from the previous day, the following is the syntaxselect *from yourTableName where date(yourColumnName)= DATE(NOW() - INTERVAL 1 DAY);To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table yesterDayRecordsDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> ArrivalDateTime datetime    -> ); Query OK, 0 rows affected (0.44 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into yesterDayRecordsDemo(ArrivalDateTime) values('2014-11-09 13:45:21'); Query OK, 1 row affected (0.11 sec) mysql> insert into yesterDayRecordsDemo(ArrivalDateTime) values('2017-10-19 11:41:31'); Query ... Read More

MySQL update a column with an int based on order?

George John
Updated on 30-Jul-2019 22:30:25

500 Views

The syntax is as follows to update a column with an int based on orderset @yourVariableName=0; update yourTableName set yourColumnName=(@yourVariableName:=@yourVariableName+1) order by yourColumnName ASC;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table updateColumnDemo    -> (    -> Id int,    -> OrderCountryName varchar(100),    -> OrderAmount int    -> ); Query OK, 0 rows affected (1.76 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into updateColumnDemo(Id, OrderCountryName) values(10, 'US'); Query OK, 1 row affected (0.46 sec) mysql> insert into updateColumnDemo(Id, OrderCountryName) ... Read More

Why should we use MySQL CASE Statement?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

126 Views

Use MySQL CASE for a fixed number of arguments.The syntax is as followsSELECT *, CASE WHEN yourColumName1>yourColumName2 THEN 'yourMessage1' ELSE 'yourMessage2' END AS anyAliasName FROM yourTableName;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table CaseFunctionDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Value1 int,    -> Value2 int    -> ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into CaseFunctionDemo(Value1, Value2) values(10, 20); Query OK, 1 row affected ... Read More

How to find and replace string in MySQL database for a particular string only?

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

2K+ Views

Use the replace() function to replace string in MySQL Database.The syntax is as followsUPDATE yourTableName SET yourColumnName=replace(yourColumnName, 'yourExistingValue', 'yourNewValue') WHERE >;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table findAndReplaceDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentFirstName varchar(20)    -> ); Query OK, 0 rows affected (0.49 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into findAndReplaceDemo(StudentFirstName) values('Carol'); Query OK, 1 row affected (0.15 sec) mysql> insert into findAndReplaceDemo(StudentFirstName) values('David'); Query OK, 1 row ... Read More

How to determine the current delimiter in MySQL?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

487 Views

To determine current delimiter in MySQL, use the following syntax\sThe above syntax will let you know about the current delimiter. Let us implement the above syntax.The query is as followsmysql> \sThe following is the output-------------- C:\Program Files\MySQL\MySQL Server 8.0\bin\mysql.exe Ver 8.0.12 for Win64 on x86_64 (MySQL Community Server - GPL) Connection id: 19 Current database: sample Current user: root@localhost SSL: Cipher in use is DHE-RSA-AES128-GCM-SHA256 Using delimiter: ; Server version: 8.0.12 MySQL Community Server - GPL Protocol version: 10 Connection: localhost via TCP/IP Insert id: 11 Server characterset: utf8 Db characterset: utf8 Client characterset: utf8 Conn. characterset: utf8 TCP port: ... Read More

GROUP BY the number of rows returned by GROUP BY in MySQL?

George John
Updated on 30-Jul-2019 22:30:25

124 Views

You can use GROUP_CONCAT() for this. To understand the above concept, let us create a table.The query to create a table is as followsmysql> create table groupByDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(100)    -> ); Query OK, 0 rows affected (1.31 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into groupByDemo(Name) values('John'); Query OK, 1 row affected (0.19 sec) mysql> insert into groupByDemo(Name) values('Carol'); Query OK, 1 row affected (0.14 sec) mysql> insert into groupByDemo(Name) values('Carol'); Query OK, 1 row affected (0.10 sec) ... Read More

MySQL query to calculate the average of values in a row?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

312 Views

To calculate the average of values in a row in MySQL, use the following syntaxSELECT (yourTableName.yourColumnName1+yourTableName.yourColumnName2+yourTableName.yourColumnName3+, ..........N)/numberOfColumns AS anyAliasName FROM yourTableName;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table calculateAverageDemo    -> (    -> x int,    -> y int,    -> z int    -> ); Query OK, 0 rows affected (1.41 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into calculateAverageDemo values(10, 20, 30); Query OK, 1 row affected (0.78 sec) mysql> insert into calculateAverageDemo values(40, 50, 70); Query ... Read More

Use MySQL concat() and lower() effectively

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

800 Views

The contact() method is used to concatenate. However, lower() is used to change the case to lowercase. For our example, let us create a table.The query to create a table is as followsmysql> create table concatAndLowerDemo    -> (    -> FirstValue varchar(10),    -> SecondValue varchar(10),    -> ThirdValue varchar(10),    -> FourthValue varchar(10)    -> ); Query OK, 0 rows affected (0.55 sec)Now you can insert some records in the table using insert command.The query is as followsmysql> insert into concatAndLowerDemo values('John', '12345', 'Java', 'MySQL'); Query OK, 1 row affected (0.21 sec) mysql> insert into concatAndLowerDemo values('Hi', '12345', ... Read More

How to avoid null result of “SELECT max(rank) FROM test” for an empty table?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

3K+ Views

You can use COALESCE() along with aggregate function MAX() for this.The syntax is as followsSELECT COALESCE(MAX(`yourColumnName`), 0) FROM yourTableName;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table avoidNullDemo    -> (    -> `rank` int    -> ); Query OK, 0 rows affected (0.53 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into avoidNullDemo values(10); Query OK, 1 row affected (0.20 sec) mysql> insert into avoidNullDemo values(NULL); Query OK, 1 row affected (0.18 sec) mysql> insert into avoidNullDemo values(20); Query OK, 1 ... Read More

What is the return type of a “count” query against MySQL using Java JDBC?

George John
Updated on 30-Jul-2019 22:30:25

364 Views

The return type of count is long. The Java statement is as followsrs.next(); long result= rs.getLong("anyAliasName");First, create a table with some records in our sample database test3. The query to create a table is as followsmysql> create table CountDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into CountDemo(Name) values('John'); Query OK, 1 row affected (0.21 sec) mysql> insert into CountDemo(Name) values('Carol'); Query OK, 1 row affected (0.16 sec) ... Read More

Advertisements