Found 4378 Articles for MySQL

Insert data from one schema to another in MySQL?

AmitDiwan
Updated on 06-Apr-2020 13:20:37

981 Views

To insert data from one scheme to another, the syntax is as follows. Here, we have two databases “yourDatabaseName1” and “yourDatabaseName2” −insert into yourDatabaseName2.yourTableName2 select *from yourDatabaseName1.yourTableName1;To understand the above syntax, let us create a table. We are creating a table in database “web” −mysql> create table DemoTable2020    -> (    -> Id int,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command −mysql> insert into DemoTable2020 values(101, 'Chris'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable2020 values(102, 'David'); Query OK, 1 ... Read More

MySQL: update field with Group By?

AmitDiwan
Updated on 06-Apr-2020 13:12:36

666 Views

To update field with GROUP BY, use ORDER BY LIMIT with UPDATE command −mysql> create table DemoTable2018    -> (    -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> EmployeeName varchar(20),    -> EmployeeSalary int    -> ); Query OK, 0 rows affected (0.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable2018(EmployeeName, EmployeeSalary) values('Chris', 10000); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable2018(EmployeeName, EmployeeSalary) values('David', 12560); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable2018(EmployeeName, EmployeeSalary) values('Chris', 25400); Query OK, 1 row affected (0.09 sec)Display all ... Read More

MySQL query to update all records to capitalize only the first letter and set all others in lowercase

AmitDiwan
Updated on 06-Apr-2020 13:07:21

686 Views

Let us first create a table −mysql> create table DemoTable2017    -> (    -> Name text    -> ); Query OK, 0 rows affected (0.55 sec)Insert some records in the table using insert command −mysql> insert into DemoTable2017 values('JOHN SMITH, MYSQL'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable2017 values('DAVID MILLER, MONGODB'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable2017 values('CHRIS BROWN, JAVA'); Query OK, 1 row affected (0.11 sec)Display all records from the table using select statement −mysql> select *from DemoTable2017;This will produce the following output −+----------------------+ | Name   ... Read More

MySQL query to concatenate records with similar corresponding ids in a single row separated by a special character

AmitDiwan
Updated on 06-Apr-2020 13:04:16

217 Views

For this, you can use CONCAT_WS() along with GROUP_CONCAT(). Let us first create amysql> create table DemoTable2016    -> (    -> UserId int,    -> UserName varchar(20)    -> ); Query OK, 0 rows affected (0.51 sec)Insert some records in the table using insert command −mysql> insert into DemoTable2016 values(1, 'Chris'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable2016 values(2, 'Bob'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable2016 values(1, 'David'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable2016 values(2, 'Carol'); Query OK, 1 row affected (0.12 ... Read More

Fetch a specific record using a single MySQL query with AND & OR operator

AmitDiwan
Updated on 06-Apr-2020 14:00:14

109 Views

Let us first create a table −mysql> create table DemoTable2015    -> (    -> StudentId int,    -> StudentName varchar(20),    -> StudentCountryName varchar(20) -> ); Query OK, 0 rows affected (1.20 sec)Insert some records in the table using insert command −mysql> insert into DemoTable2015 values(1, 'Chris', 'US'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable2015 values(2, 'Bob', 'UK'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable2015 values(3, 'David', 'AUS'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable2015 values(4, 'Robert', 'US'); Query OK, 1 ... Read More

Set 'alias' for all the column names in a single MySQL query

AmitDiwan
Updated on 06-Apr-2020 12:55:44

248 Views

To set alias for column names, the syntax is as follows −select yourColumnName1 anyAliasName1, yourColumnName2 anyAliasName2 from yourTableName anyAliasName;To understand the above syntax, let us create a table −mysql> create table DemoTable2014    -> (    -> FirstName varchar(20),    -> LastName varchar(20)    -> ); Query OK, 0 rows affected (0.70 sec)Insert some records in the table using insert command −mysql> insert into DemoTable2014 values('John', 'Smith'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable2014 values('David', 'Miller'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable2014 values('John', 'Doe'); Query OK, ... Read More

Set MySQL select in a custom variable

AmitDiwan
Updated on 06-Apr-2020 12:51:59

324 Views

Let us first create a table −mysql> create table DemoTable2013    -> (    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable2013 values('Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable2013 values('David'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable2013 values('Mike'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable2013 values('Sam'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable2013 values('Bob'); Query OK, 1 row affected (0.12 sec)Display all ... Read More

Format date while inserting records in MySQL

AmitDiwan
Updated on 06-Apr-2020 12:50:12

443 Views

To format date while inserting records, use DATE_FORMAT() in the MySQL INSERT statement. Let us first create a table −mysql> create table DemoTable2012    -> (    -> ShippingDate varchar(20)    -> ); Query OK, 0 rows affected (0.48 sec)Insert some records in the table using insert command −mysql> insert into DemoTable2012 values(date_format(curdate(), '%d.%m.%Y')); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable2012 values(date_format(now(), '%d.%m.%Y')); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable2012 values(date_format('2014-01-21', '%d.%m.%Y')); Query OK, 1 row affected (0.17 sec)Display all records from the table using select statement −mysql> select *from ... Read More

MySQL stored procedure to execute SHOW CREATE TABLE?

AmitDiwan
Updated on 06-Apr-2020 12:48:14

364 Views

To execute SHOW CREATE TABLE in a stored procedure, use SHOW CREATE TABLE. Let us first create a table −mysql> create table DemoTable2011    -> (    -> StudentId int NOT NULL AUTO_INCREMENT,    -> StudentName varchar(20),    -> StudentAge int,    -> StudentCountryName varchar(20),    -> PRIMARY KEY(StudentId)    -> ); Query OK, 0 rows affected (0.80 sec)Following is the stored procedure executing SHOW CREATE TABLE −mysql> delimiter // mysql> create procedure test_show_create_demo(table_name varchar(100))    -> begin    -> set @query=concat("SHOW CREATE TABLE ", table_name);    -> prepare st from @query;    -> execute st;    -> end   ... Read More

How to properly use 'exist' function in MongoDB like in SQL?

AmitDiwan
Updated on 27-Mar-2020 12:34:34

105 Views

To check for existence of a record, use findOne() in MongoDB. Let us first create a collection with documents −> db.existsAlternateDemo.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06d23f9e4dae213890ac5c") } > db.existsAlternateDemo.insertOne({"StudentName":"Chris", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06d2559e4dae213890ac5d") } >db.existsAlternateDemo.insertOne({"StudentName":"Chris", "StudentAge":22, "StudentCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06d2689e4dae213890ac5e") }Following is the query to display all documents from a collection with the help of find() method −> db.existsAlternateDemo.find();This will produce the following output −{ "_id" : ObjectId("5e06d23f9e4dae213890ac5c"), "StudentName" : "Chris" } { "_id" : ObjectId("5e06d2559e4dae213890ac5d"), "StudentName" : "Chris", "StudentAge" : 21 } { ... Read More

Advertisements