Found 4381 Articles for MySQL

MySQL queries to update date records with NULL values

Sharon Christine
Updated on 30-Jul-2019 22:30:26

1K+ Views

You can use IFNULL() for this. Let us first create a table −mysql> create table DemoTable -> ( -> added_date date, -> updated_date date -> ); Query OK, 0 rows affected (0.95 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-10', '2019-06-01'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('2019-05-19', NULL); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(NULL, '2019-09-05'); Query OK, 1 row affected (0.18 sec)Display all records from the table using ... Read More

How to use special characters in column names with MySQL?

karthikeya Boyini
Updated on 30-Jun-2020 09:38:05

2K+ Views

Using backticks around the column name will allow you to use special characters. Let us first create a table −mysql> create table DemoTable    -> (    -> `Student-Id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> `Student-Name` varchar(100),    -> `Student-Age` int    -> ); Query OK, 0 rows affected (0.55 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(`Student-Name`, `Student-Age`) values('Chris', 21); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(`Student-Name`, `Student-Age`) values('Mike', 19); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(`Student-Name`, `Student-Age`) values('Bob', 18); Query OK, ... Read More

Set format specifier in MySQL STR_TO_DATE() and convert string to date

karthikeya Boyini
Updated on 30-Jun-2020 09:39:31

283 Views

Specify format specifier in STR_TO_DATE() method that convertes string to date. Following is the syntax −select STR_TO_DATE(yourColumnName, 'yourFormatSpecifier') from yourTableName;Let us first create a table −mysql> create table DemoTable -> ( -> AdmissionDate varchar(100) -> ); Query OK, 0 rows affected (0.55 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('01-12-2019'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('15-06-2019'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('21-01-2016'); Query OK, 1 row affected (0.19 sec)Display all records from the table using select statement −mysql> select *from ... Read More

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

karthikeya Boyini
Updated on 30-Jun-2020 09:16:47

491 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?

Sharon Christine
Updated on 30-Jul-2019 22:30:26

990 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?

Sharon Christine
Updated on 31-Oct-2023 21:06:09

63K+ 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?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

86 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

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

142 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?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

1K+ 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

Sharon Christine
Updated on 30-Jun-2020 09:26:02

671 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

Advertisements