Found 4378 Articles for MySQL

How to add multiple intervals to DATE_ADD() in MySQL?

Naveen Singh
Updated on 30-Jun-2020 09:16:47

397 Views

The current date and time is as follows −mysql> select now();OutputThis will produce the following output −+---------------------+ | now()               | +---------------------+ | 2019-06-15 12:24:06 | +---------------------+ 1 row in set (0.00 sec)Let us first create a table −mysql> create table DemoTable    ->(    -> ArrivalDate datetime    -> ); Query OK, 0 rows affected (1.15 sec)Insert some records in the table using insert command. Here, we are adding multiple intervals to the DATE_ADD() method −mysql> insert into DemoTable values(DATE_ADD(DATE_ADD(NOW(), INTERVAL 6 MONTH), INTERVAL 1 YEAR)); Query OK, 1 row affected (0.13 sec) ... Read More

How to count the number of occurrences of a specific value in a column with a single MySQL query?

Naveen Singh
Updated on 30-Jul-2019 22:30:26

822 Views

For this, you can use GROUP BY clause along with IN(). Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(100)    -> ); Query OK, 0 rows affected (0.87 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name) values('John'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable(Name) values('Chris'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Name) values('David'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable(Name) values('Chris'); Query ... Read More

Fix ERROR 1064 (42000) while creating a database in MySQL?

Naveen Singh
Updated on 31-Oct-2023 21:06:09

53K+ Views

The ERROR 1064 (42000) mainly occurs when the syntax isn’t set correctly i.e. error in applying the backtick symbol or while creating a database without them can also create an error, if you will use hyphen in the name, for example, Demo-Table will result in ERROR 1064 (42000).To remove the error, you need to use backtick around the database name properly or use nothing. Following is the syntax wherein we haven’t used the backtick. This works correctly −create database yourDatabaseName;Since adding hyhen to the database name will result in an error. Let us implement it while creating the database name ... Read More

Averaging a total from a Score column in MySQL with the count of distinct ids?

Naveen Singh
Updated on 30-Jul-2019 22:30:26

53 Views

You can use DISTINCT along with COUNT(). Let us first create a table −mysql> create table DemoTable    -> (    -> Id int,    -> Score int    -> ); Query OK, 0 rows affected (0.95 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10, 90); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(10, 190); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(11, 230); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(11, 130); Query OK, 1 row affected (0.17 ... Read More

Determine what a MySQL DB’s charset is set to

Naveen Singh
Updated on 30-Jul-2019 22:30:26

90 Views

Let’s say we are creating a database “web” −mysql> SHOW CREATE DATABASE web;This will produce the following output displaying the default charset as well −+----------+-----------------------------------------------------------------------------------------+ | Database | Create Database                                                                         | +----------+-----------------------------------------------------------------------------------------+ | web      | CREATE DATABASE `web` /*!40100 DEFAULT CHARACTER SET utf8 COLLATEutf8_unicode_ci */ | +----------+-----------------------------------------------------------------------------------------+ 1 row in set (0.03 sec)If you want to know ... Read More

Auto insert values into a MySQL table in a range?

Naveen Singh
Updated on 30-Jul-2019 22:30:26

945 Views

For this, you can create a stored procedure. Let us first create a table.mysql> create table DemoTable    -> (    -> Value int    -> ); Query OK, 0 rows affected (0.55 sec)Following is the query to create a stored procedure to auto insert values to a table from range 10 to 20 −mysql> DELIMITER // mysql> CREATE PROCEDURE AutoInsertValuesToTable()    -> BEGIN    ->    DECLARE startingRange INT DEFAULT 10;    ->    WHILE startingRange       INSERT DemoTable(Value) VALUES (startingRange );    ->       SET startingRange = startingRange + 1;    ->   ... Read More

Selecting and dividing numbers in a column and displaying the decimal result in integer with MySQL

Naveen Singh
Updated on 30-Jun-2020 09:26:02

487 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Number int    -> ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Number) values(10); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Number) values(20); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Number) values(30); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(Number) values(40); Query OK, 1 row affected (0.14 sec) Display all records from ... Read More

How to exclude a specific row from a table in MySQL?

Naveen Singh
Updated on 30-Jun-2020 09:28:04

502 Views

Use i.e. not equal in MySQL to exclude a specific row from a table. Let us first create a table −mysql> create table DemoTable    -> (    -> Id int,    -> FirstName varchar(100)    -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(1, 'John'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(2, 'Chris'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(3, 'Robert'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable ... Read More

Display records with more than two occurrences in MySQL?

Naveen Singh
Updated on 30-Jul-2019 22:30:26

612 Views

For this, you can use GROUP BY HAVING clause. Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Subject varchar(100)    -> ); Query OK, 0 rows affected (0.53 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Subject) values('MySQL'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(Subject) values('MongoDB'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable(Subject) values('MySQL'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(Subject) values('Java'); Query OK, 1 ... Read More

Concatenating two strings in MySQL with space?

Naveen Singh
Updated on 30-Jul-2019 22:30:26

954 Views

For this, you can use concat() function from MySQL. Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(20),    -> Subject varchar(100)    -> ); Query OK, 0 rows affected (17.73 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name, Subject) values('John', 'MySQL'); Query OK, 1 row affected (1.19 sec) mysql> insert into DemoTable(Name, Subject) values('Chris', 'SQL Server'); Query OK, 1 row affected (0.88 sec) mysql> insert into DemoTable(Name, Subject) values('Robert', 'MongoDB'); Query OK, 1 row affected ... Read More

Advertisements