Question of the month October 2017 – What is the use of underscores in comparison?

In MySQL, Let us consider the following example

SELECT * FOM table
WHERE col like ‘_____’

What is the use of underscores here? How does it work?

Advertisements

MySQL – Simple ways to know version number with @@version and version()

If you want to know the version number of MySQL server, there are two simple methods available.

You can use system variable @@VERSION as shown below

SELECT @@VERSION as mysql_version;

Other method is to use system function VERSION()

SELECT VERSION() as mysql_version

Both of them returns the following result

mysql_version
-----------------
5.6.35

Depends on the version of the server, you may get the different version number

Note : @@VERSION is already available in SQL Server database engine as a global variable to get the version information

MySQL – Concatenate column values for each group with GROUP_CONCAT function

As I said in my earlier posts, MySQL has some interesting features that make the things simpler. GROUP_CONCAT is one of such functions

This function concatenates values from several rows into a single row separated by the delimiter specified.

For example consider the following example with simple data set

create table customer_visits(customer_id int, place_name varchar(100));

insert into customer_visits (customer_id,place_name)
select 1,'Chennai' union all
select 1,'Denmark' union all
select 1,'USA' union all
select 2,'UK' union all
select 2,'Argentina'

Now for each customer id, if you want to concatenate the visited places in a single string separated by comma, you can use the following code

select customer_id,
       GROUP_CONCAT(place_name) as visited_places
from
       customer_visits
group by
       customer_id

The result is

customer_id      visited_places
1                Chennai,Denmark,USA
2                UK,Argentina

Note that by default values are concatenated using comma. If you want to use different delimiter, say for example a semicolon (;), you can use SEPARATOR clause to do it as shown below.

select customer_id,
       GROUP_CONCAT(place_name SEPARATOR ';') as visited_places
from
       customer_visits
group by
      customer_id

The result is

customer_id      visited_places
1                Chennai;Denmark;USA
2                UK;Argentina

GROUP_CONCAT is really a quite handy function.

MySQL – UPSERT in a single statement

MySQL, an opensource RDBMS, has lot of interesting features. One of them is ON DUPLICATE KEY UPDATE statement. This statement adds a new row, if the unique or primary column is not available in the table, if available,UPDATE the row.

Consider the following dataset

create table test
(
       id int ,
       name varchar(100),
       status varchar(100)
);

CREATE unique INDEX id ON test (id);

Now add the following rows

insert into test(id,name,status)
values (1,'test1','') ;

insert into test(id,name,status)
values (2,'test2','') ;

If you select the rows

select * from test

It returns the following rows

id           name           status
1            test1
2            test2

When you try to add the following data

insert into test(id,name,status)
values (1,'test3','') ;

You get the following error

Duplicate entry ‘1’ for key ‘id’

Suppose if there exists a row for the id, you want update it, say status column with the value “duplicate” and not to add as new row. You can use ON DUPLICATE KEY UPDATE statement as shown below;

insert into test(id,name,status)
values (1,'test3','')
ON DUPLICATE KEY UPDATE status='duplicate';

If you select the rows

select * from test

It returns the following rows

id           name           status
1            test1         duplicate
2            test2

As you see new row is not added but status column of the row with id value 1 is updated with the value duplicate