AmitDiwan has Published 10740 Articles

Order by multiple columns not working as expected in MySQL?

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 12:25:28

1K+ Views

Following is the syntax to order by multiple columns −select *from yourTableName order by yourColumnName1 DESC, yourColumnName2, yourColumnName3;Let us create a table −mysql> create table demo29 −> ( −> value1 int, −> value2 int −> ); Query OK, 0 rows affected (1.67 sec)Insert some records into the table with the ... Read More

How to select record except the lower value record against a specific value in MySQL?

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 12:20:28

146 Views

For this, you need to use the WHERE clause. Following is the syntax −select *from yourTableName where yourColumnName > yourValue;Let us create a table −mysql> create table demo27 −> ( −> id int not null auto_increment primary key, −> value int −> ); Query OK, 0 rows affected (3.14 sec)Insert ... Read More

How to replace only the first repeated value in a string in MySQL

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 12:17:26

538 Views

For this, you can use REGEXP_REPLACE(). Let’s say our string is −This is my first MySQL query. This is the first tutorial. I am learning for the first time.We need to replace only the 1st occurrence of a specific word, let’s say “first”. The output should be −This is my ... Read More

Springboot + JSP + Spring Security: Failed to configure a DataSource. How to configure DataSource in MySQL?

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 12:12:42

174 Views

To configure a DataSource in Springboot, you can define DataSource into application.properties.The application.properties is as follows for Springboot −spring.datasource.username=yourUserName spring.datasource.password=yourPassword spring.datasource.url=yourDatabaseUrl spring.datasource.driver-class-name=yourDriverClassNameThe project structure is as follows −ExampleTo understand the above concept, let us create a controller class with spring boot. The Java code is as follows −package com.demo.controller; import ... Read More

Set default value to a JSON type column in MySQL?

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 12:09:29

2K+ Views

To set default value, use the DEFAULT constraint as in the below syntax −alter table yourTableName modify column yourColumnName JSON NOT NULL DEFAULT ( JSON_OBJECT() );Let us create a table −mysql> create table demo24 −> ( −> employee_information text −> ) −> ; Query OK, 0 rows affected (1.43 sec)Here ... Read More

MySQL query to fetch the maximum cumulative value

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 12:02:07

310 Views

For this, use aggregate function COUNT(*) along with subquery. GROUP BY is also used.Let us create a table −mysql> create table demo23 −> ( −> id int not null auto_increment primary key, −> value1 int, −> value2 int −> ); Query OK, 0 rows affected (1.65 sec)Insert some records into ... Read More

Insert the data into Table C IF The data is not in Table B while Comparing to Table A in MySQL?

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 11:59:33

183 Views

For this, use left join on table A and B. Let us create the first table −mysql> create table demo20 −> ( −> id int, −> name varchar(20) −> ); Query OK, 0 rows affected (1.87 sec)Insert some records into the table with the help of insert command −mysql> insert ... Read More

How to use quotes correctly while using LIKE with search variable in MySQL in Java?

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 11:34:29

615 Views

Following is the correct syntax to use LIKE with search variable −String sqlQuery; sqlQuery = "select *from yourTableName where yourColumnName like '%" +yourSearchVariableName + "%'";Let us create a table −mysql> create table demo19 −> ( −> id int not null auto_increment primary key, −> name varchar(50) −> ); Query OK, ... Read More

Sort in a string mixed with numbers in MySQL?

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 11:29:49

785 Views

Use ORDER BY with some cases. Let us create a table −mysql> create table demo18 −> ( −> value text −> ); Query OK, 0 rows affected (1.18 sec)Insert some records into the table with the help of insert command −mysql> insert into demo18 values('John Smith'); Query OK, 1 row ... Read More

Combine SELECT & SHOW command results in MySQL?

AmitDiwan

AmitDiwan

Updated on 19-Nov-2020 11:28:09

379 Views

To combine SELECT and SHOW command results into one, use the below query −select @anyVariableName1 as anyAliasName1, @anyVariableName1 as anyAliasName2, ......N;To combine the SELECT and SHOW, first create and initialize the first variable. Following is the query −mysql> set @first_name='John'; Query OK, 0 rows affected (0.00 sec)To combine the SELECT ... Read More

Advertisements