AmitDiwan has Published 10744 Articles

Update the date and time values while inserting them in MySQL

AmitDiwan

AmitDiwan

Updated on 21-Aug-2019 11:55:41

149 Views

Here, we will see an example wherein we are inserting datetime and updating them while using INSERT query.Let us first create a table −mysql> create table DemoTable816 (DueDate datetime); Query OK, 0 rows affected (0.45 sec)Insert some records in the table using insert command. Here is the query to add ... Read More

Copy from one column to another (different tables same database) in MySQL?

AmitDiwan

AmitDiwan

Updated on 21-Aug-2019 11:49:55

6K+ Views

To copy from one column to another, you can use INSERT INTO SELECT statement.Let us first create a table −mysql> create table DemoTable1 (PlayerScore int); Query OK, 0 rows affected (0.46 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(98); Query OK, 1 row affected ... Read More

MySQL query to set my auto increment column ( id ) to zero or reset the value of auto increment field?

AmitDiwan

AmitDiwan

Updated on 21-Aug-2019 11:48:17

3K+ Views

Use ALTER table to set the auto_increment column to 0 or reset with another valueALTER TABLE yourTableName AUTO_INCREMENT=0;The above syntax will begin from 1.Let us first create a table −mysql> create table DemoTable698 (    ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY ) auto_increment=109; Query OK, 0 rows affected (0.88 ... Read More

How to select date from timestamp in MySQL?

AmitDiwan

AmitDiwan

Updated on 21-Aug-2019 11:47:10

432 Views

To select date from timestamp in MySQL, you need to use DATE().Let us first create a table −mysql> create table DemoTable697(    Id varchar(100),    Title varchar(100),    BatchTime timestamp ); Query OK, 0 rows affected (0.78 sec)Insert some records in the table using insert command −mysql> insert into DemoTable697 ... Read More

How to return table from MySQL function?

AmitDiwan

AmitDiwan

Updated on 21-Aug-2019 11:44:57

9K+ Views

You cannot return table from MySQL function. The function can return string, integer, char etc. To return table from MySQL, use stored procedure, not function.Let us first create a table −mysql> create table DemoTable696 (    Id int,    Name varchar(100) ); Query OK, 0 rows affected (0.77 sec)Insert some ... Read More

How to implement Count (*) as variable from MySQL to display the number of records in a table?

AmitDiwan

AmitDiwan

Updated on 21-Aug-2019 11:43:02

1K+ Views

The alias name can be used as a variable name in MySQL as shown in the below syntax −select count(*) AS anyAliasName from yourTableName;Let us first create a table −mysql> create table DemoTable695 (    FirstName varchar(100) ); Query OK, 0 rows affected (0.72 sec)Insert some records in the table ... Read More

How to get the first and last record of the table in MySQL?

AmitDiwan

AmitDiwan

Updated on 21-Aug-2019 11:41:46

18K+ Views

To get the first and last record, use UNION. LIMIT is also used to get the number of records you want.Let us first create a table −mysql> create table DemoTable694 (    EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    EmployeeName varchar(100),    EmployeeSalary int ); Query OK, 0 rows ... Read More

Can we use “When” as column name in CREATE TABLE statement?

AmitDiwan

AmitDiwan

Updated on 21-Aug-2019 11:39:49

113 Views

Before beginning, let us try to set ‘when’ as column name while using CREATE TABLE statement −mysql> create table DemoTable693(    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(100),    When datetime );This will produce the following output. An error would be visible:ERROR 1064 (42000): You have an ... Read More

Get a list of MySQL databases and version?

AmitDiwan

AmitDiwan

Updated on 21-Aug-2019 11:36:21

152 Views

To get a list of MySQL databases, following is the syntax -show databases;To get the server version, you can use the below syntax -select version();Let us implement the above syntax to get a list of MySQL databases and version -mysql> show databases;This will produce the following output displaying all the ... Read More

How to group by date regardless of time in MySQL?

AmitDiwan

AmitDiwan

Updated on 21-Aug-2019 11:33:06

493 Views

When you have identical dates in a table with different time values for each, you can group them easily with GROUP BY DATE.Let us first create a table -mysql> create table DemoTable692 (DueDatetime datetime); Query OK, 0 rows affected (0.97 sec)Insert some records in the table using insert command:mysql> insert ... Read More

Advertisements