Found 4219 Articles for MySQLi

MySQL syntax not evaluating with not equal operator in presence of null?

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:24

113 Views

Use the IS NOT NULL operator to compare with NULL values. The syntax is as follows −SELECT *FROM yourTableName where yourColumnName1 is not null or yourColumnName2 anyIntegerValue;To check the not equal to in presence of null, let us create a table. The query to create a table is as follows −mysql> create table IsNullDemo    −> (    −> ProductId int,    −> ProductName varchar(100),      −> ProductBackOrder int    −> ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table with null value to avoid the presence of null. The query to insert records ... Read More

Getting last 5 character of a string with MySQL query?

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:24

18K+ Views

To get the first n characters of string with MySQL, use LEFT(). To get the last n char of string, the RIGHT() method is used in MySQL.The syntax for RIGHT() method is as follows −SELECT RIGHT(yourColumnName, valueOfN) as anyVariableName from yourTableName;To understand the above concept, let us create a table. The query to create a table is as follows −mysql> create table gettingLast5Characters    −> (    −> BookName varchar(100)    −> ); Query OK, 0 rows affected (0.73 sec)Now you can insert records in the table using insert command. The query is as follows −mysql> insert into gettingLast5Characters values('Introduction ... Read More

How to order records by a column in MySQL and place empty records at the end?

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

165 Views

To get order by column and place empty records at the end, use ORDER By and “is null” from MySQL. The syntax is as follows −select *from yourTableName order by if(yourColumName = ’ ’ or yourColumName is null, 1, 0), yourColumnName;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table emptyCellsAtEnd    −> (    −> ProductId varchar(100)    −> ); Query OK, 0 rows affected (0.65 sec)Insert some records in the table using insert command. Some of these records are empty. The query is as follows −mysql> ... Read More

Display all tables inside a MySQL database using Java?

Rishi Rathor
Updated on 30-Jul-2019 22:30:24

1K+ Views

We will see here how to display all tables inside a MySQL database using Java. You can use show command from MySQL to get all tables inside a MySQL database.Let’s say our database is ‘test’. The Java code is as follows to show all table names inside a database ‘test’.The Java code is as follows. Here, connection is established between MySQL and Java −import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import com.mysql.jdbc.Connection; import com.mysql.jdbc.DatabaseMetaData; public class GetAllTables {    public static void main(String[] args) throws SQLException {       Connection conn = null;       try { ... Read More

How to bulk update MySQL data with a single query?

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:24

6K+ Views

You can bulk update MySQL data with one query using CASE command. The syntax is as follows −update yourTableName set yourUpdateColumnName = ( Case yourConditionColumnName WHEN Value1 THEN ‘’UpdatedValue’ WHEN Value2 THEN ‘UpdatedValue’ . . N END) where yourConditionColumnName IN(Value1, Value2, .....N);To understand the above concept, let us create a table. The query to create a table is as follows −mysql> create table UpdateAllDemo −> ( −> BookId int, −> BookName varchar(200) −> ); Query OK, 0 rows affected (1.18 sec)Insert some records in the table using insert ... Read More

How to select domain name from email address in MySQL?

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:24

4K+ Views

To select domain name from email address, you can use in-built SUBSTRING_INDEX() function from MySQL.To understand the concept, let us create a table. The following is the query to create a table.mysql> create table selectDomainNameOnly −> ( −> UserEmailAddress varchar(200) −> ); Query OK, 0 rows affected (0.52 sec)Insert records in the table using insert command. The record will have email-ids from which we need to fetch the domain name. The query is as follows −mysql> insert into selectDomainNameOnly values('John123@yahoo.com'); Query OK, 1 row affected (0.10 sec) mysql> insert into selectDomainNameOnly ... Read More

Set column charset in MySQL?

Rishi Rathor
Updated on 30-Jul-2019 22:30:24

455 Views

Set column charset using character SET command. The syntax is as follows −ALTER TABLE yourTableName MODIFY youColumName type CHARACTER SET anyCharcaterSetName;You can use character set name utf8 or something elsE. To set column charset, let us first create a table. The query to create a table is as follows −mysql> create table setCharsetDemo −> ( −> FirstName varchar(60) −> ); Query OK, 0 rows affected (2.09 sec)Now you can check the current column character set with the help of show command. The query is as follows −mysql> show create table setCharsetDemo;The ... Read More

Count number of times value appears in particular column in MySQL?

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

10K+ Views

You can use aggregate function count() with group by. The syntax is as follows.select yourColumnName, count(*) as anyVariableName from yourtableName group by yourColumnName;To understand the above syntax, let us create a table. The query to create a table is as follows.mysql> create table CountSameValue -> ( -> Id int, -> Name varchar(100), -> Marks int -> ); Query OK, 0 rows affected (0.70 sec)Insert records in the table using insert command. The query is as follows.mysql> insert into CountSameValue values(1, 'Sam', 67); Query OK, 1 row affected (0.17 sec) mysql> insert into CountSameValue values(2, 'Mike', 87); Query OK, 1 ... Read More

How to check for duplicates in MySQL table over multiple columns?

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

1K+ Views

To check for duplicates in MySQL, you can use group by having clause. The syntax is as follows.select yourColumnName1, yourColumnName2, ......N, count(*) as anyVariableName from yourTableName group by yourColumnName1, yourColumnName2 having count(*) > 1;To understand the above syntax, let us create a table. The query to create a table is as follows.mysql> create table DuplicateDemo -> ( -> StudentId int not null, -> StudentFirstName varchar(100), -> StudentLastName varchar(100), -> Primary Key(StudentId) -> ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert command. The query is as follows.mysql> insert into DuplicateDemo values(1, 'John', 'Smith'); Query ... Read More

How to select sum or 0 if no records exist in MySQL?

Chandu yadav
Updated on 29-Jun-2020 12:27:22

1K+ Views

You can use aggregate function sum() inside COALESCE(). The below syntax returns the sum of all if the record exists otherwise 0 is returned. The syntax is as follows.select COALESCE(sum(yourColumnName2), 0) AS anyVariableName from yourTableName where yourColumnName1 like '%yourValue%';To understand the above syntax, let us create a table. The query to create a table is as follows.mysql> create table SumDemo -> ( -> Words varchar(100), -> Counter int -> ); Query OK, 0 rows affected (0.93 sec)Insert some records in the table using insert command. The query is as follows.mysql> insert into SumDemo values('Are You There', 10); Query OK, 1 ... Read More

Advertisements