Found 4378 Articles for MySQL

Getting last value in MySQL group concat?

AmitDiwan
Updated on 11-Dec-2019 06:39:32

542 Views

To get last value in group concat, use SUBSTRING_INDEX(). Let us first create a table −mysql> create table DemoTable1525    -> (    -> ListOfSubjects text    -> ); Query OK, 0 rows affected (1.13 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1525 values('MongoDB, C'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1525 values('Java, C++, MySQL'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable1525 values('Python, C++, C, Java'); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> select * from DemoTable1525;This will ... Read More

Count the number of columns in a MySQL table with Java

AmitDiwan
Updated on 11-Dec-2019 06:37:17

290 Views

For this, use ResultSetMetaData. Let us first create a table −mysql> create table DemoTable    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentFirstName varchar(20),    -> StudentLastName varchar(20)    -> ); Query OK, 0 rows affected (0.58 sec)The Java code is as follows −Exampleimport java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import com.mysql.jdbc.ResultSetMetaData; public class ResultSetDemo {    public static void main(String[] args) {       Connection con = null;       PreparedStatement ps = null;       Statement st = null;       ResultSet rs = null; ... Read More

Match the elements of an array in a MySQL query

AmitDiwan
Updated on 11-Dec-2019 06:33:22

2K+ Views

Let us first create a table table −mysql> create table DemoTable1523    -> (    -> Id int,    -> Value int    -> ); Query OK, 0 rows affected (0.76 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1523 values(1, 56); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1523 values(2, 78); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1523 values(1, 34); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1523 values(2, 45); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1523 values(1, 99); Query OK, ... Read More

Sum columns corresponding values according to similar dates in MySQL?

AmitDiwan
Updated on 11-Dec-2019 06:31:06

127 Views

For this, use aggregate function SUM() along with GROUP BY. Let us first create a table −mysql> create table DemoTable1522    -> (    -> ProductPurchaseDate date,    -> NumberOfProduct int    -> ); Query OK, 0 rows affected (1.51 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1522 values('2019-01-21', 45); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1522 values('2018-12-31', 78); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1522 values('2019-01-21', 67); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1522 values('2019-03-01', 56); Query OK, 1 row affected (0.19 ... Read More

Update a column in MySQL and remove the trailing underscore values

AmitDiwan
Updated on 11-Dec-2019 06:27:28

417 Views

To remove the trailing values, use TRIM() as in the below update syntax −update yourTableName set yourColumnName=trim(trailing '_' from yourColumnName);Let us first create a table −mysql> create table DemoTable1521    -> (    -> StudentCode varchar(20)    -> ); Query OK, 0 rows affected (1.33 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1521 values('345_'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1521 values('12345'); Query OK, 1 row affected (0.38 sec) mysql> insert into DemoTable1521 values('9084_'); Query OK, 1 row affected (1.29 sec)Display all records from the table using select statement −mysql> select ... Read More

Take all records from one MySQL table and insert it to another?

AmitDiwan
Updated on 11-Dec-2019 06:25:16

130 Views

For this, you can use the concept of CREATE TABLE AS SELECT statement. Let us first create a table −mysql> create table DemoTable1518    -> (    -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> EmployeeName varchar(20)    -> )AUTO_INCREMENT=101; Query OK, 0 rows affected (0.69 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1518(EmployeeName) values('John Doe'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1518(EmployeeName) values('John Smith'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1518(EmployeeName) values('David Miller'); Query OK, 1 row affected (0.14 sec)Display all records from the ... Read More

How do I select data from one table only where column values from that table match the column values of another table in MySQL?

AmitDiwan
Updated on 11-Dec-2019 06:23:25

4K+ Views

For this, you can use subquery along with EXISTS. Let us first create a table −mysql> create table DemoTable1    -> (    -> Id int,    -> SubjectName varchar(20)    -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(111, 'MySQL'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1 values(112, 'MongoDB'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1 values(113, 'Java'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1 values(114, 'C'); Query OK, 1 row affected (0.27 sec) ... Read More

Counting with condition in MySQL?

AmitDiwan
Updated on 11-Dec-2019 06:19:44

852 Views

To count, use aggregate function SUM() and to count with condition, you need to set the condition with WHERE. Let us first create a table −mysql> create table DemoTable1515    -> (    -> ClientId varchar(10),    -> ClientName varchar(20)    -> ); Query OK, 0 rows affected (0.53 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1515 values('CLI-101', 'Chris'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1515 values('CLI-110', 'David'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1515 values('CLI-101', 'Bob'); Query OK, 1 row affected (0.18 sec) mysql> insert into ... Read More

MySQL query to select rows one batch at a time

AmitDiwan
Updated on 11-Dec-2019 06:15:48

2K+ Views

For this, you can use the concept of LIMIT and OFFSET. Let us first create a table −mysql> create table DemoTable1514    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> FirstName varchar(20)    -> ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1514(FirstName) values('Chris'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1514(FirstName) values('Bob'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1514(FirstName) values('Sam'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable1514(FirstName) values('Mike'); Query OK, 1 row ... Read More

When inserting a new row, should I include the columns that are null in the MySQL query?

AmitDiwan
Updated on 11-Dec-2019 06:14:44

84 Views

If you do not specify the column list in insert statement then you can use below syntax −insert into yourTableName values(NULL, yourValue, NULL, NULL, .....N);Let us first create a table −mysql> create table DemoTable1513    -> (    -> StudentId int,    -> StudentName varchar(20) ,    -> StudentAge int,    -> StudentCountryName varchar(20)    -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1513 values(NULL, 'Chris Brown', NULL, NULL); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1513 values(101, NULL, NULL, NULL); Query OK, 1 row ... Read More

Advertisements