Found 4378 Articles for MySQL

How to set time data type to be only HH:MM in MySQL?

Samual Sam
Updated on 30-Jul-2019 22:30:25

3K+ Views

You can use DATE_FORMAT() to set time data type to be only HH:MM. Following is the syntax −select DATE_FORMAT(yourColumnName, "%H:%i") AS anyAliasName from yourTableName;Let us first create a table −mysql> create table DemoTable (    Arrivaltime time ); Query OK, 0 rows affected (0.61 sec)Insert records in the table using insert command −mysql> insert into DemoTable values('08:20'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('05:40'); Query OK, 1 row affected (0.12 sec)Following is the query to display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+-------------+ | Arrivaltime ... Read More

How to insert into two tables using a single MySQL query?

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

5K+ Views

You can use stored procedure to insert into two tables in a single query. Let us first create a table −mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentFirstName varchar(20) ); Query OK, 0 rows affected (0.56 sec)Here is the query to create second table −mysql> create table DemoTable2 (    ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    ClientName varchar(20),    ClientAge int ); Query OK, 0 rows affected (0.76 sec)Following is the query to create stored procedure to insert into two tables created above −mysql> DELIMITER //    mysql> CREATE PROCEDURE insert_into_twoTables(name ... Read More

Perform complex MySQL insert by using CONCAT()?

Samual Sam
Updated on 30-Jul-2019 22:30:25

1K+ Views

To perform complex MySQL insert, you can use CONCAT() function. Let us see an example and create a table with StudentId and StudentFirstName.After that, complex MySQL insert will be performed and 'Web Student’ text will get inserted for every value and unique StudentId will get concatenated.The query to create first table is as follows −mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentFirstName varchar(20) ); Query OK, 0 rows affected (0.55 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentFirstName) values('John'); Query OK, 1 row affected (0.16 sec) mysql> insert ... Read More

How to select the table with the greatest number of columns in MySQL?

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

92 Views

You can use INFORMATION_SCHEMA.COLUMNS to get the table with the greatest number of columns. The syntax is as follows −SELECT TABLE_NAME, COUNT(*) AS anyAliasName FROM INFORMATION_SCHEMA.COLUMNS GROUP BY TABLE_NAME ORDER BY yourAliasName DESC LIMIT 1;Following is the query to select the table that has the greatest number of columns. We are getting this result because we have set the count to DESC and used GROUP BY TABLE_NAME −mysql> SELECT TABLE_NAME, COUNT(*) as TOTAL_COUNT FROM INFORMATION_SCHEMA.COLUMNS GROUP BY TABLE_NAME ORDER BY TOTAL_COUNT DESC LIMIT 1;This will produce the following output −+-----------------------------------+-------------+ | TABLE_NAME                   ... Read More

How to order last 5 records by ID in MySQL

Samual Sam
Updated on 30-Jul-2019 22:30:25

288 Views

You can use subquery for this. Let us first create a table −mysql> create table DemoTable (    Id int ); Query OK, 0 rows affected (0.68 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(115); Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable values(200); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values(140); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(124); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable ... Read More

Display 3 decimal places in MySQL?

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

1K+ Views

To display 3 digits after decimal, use TRUNCATE(). Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Value DECIMAL(10, 5) ); Query OK, 0 rows affected (0.51 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Value) values(109.4567); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable(Value) values(15.9875); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable(Value) values(1234.2346789); Query OK, 1 row affected, 1 warning (0.14 sec)Following is the query to display all records from the table using select statement −mysql> select ... Read More

How to extract part of a URL in MySQL?

Samual Sam
Updated on 30-Jul-2019 22:30:25

2K+ Views

You need to use SUBSTRING_INDEX() function from MySQL to extract part of a URL. Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    URL text ); Query OK, 0 rows affected (0.53 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(URL) values('https:\www.example.com\homepage'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable(URL) values('https:\www.onlinetest.com\welcome\indexpage'); Query OK, 1 row affected (0.12 sec)Following is the query to display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output. Here, we can ... Read More

Which is the fastest method to get the total row count for a MySQL Query?

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

149 Views

You can use subquery with aggregate COUNT(*) to get the total row count. Let us first create a table −mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentFirstName varchar(20),    StudentAge int ); Query OK, 0 rows affected (0.48 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentFirstName, StudentAge) values('John', 23); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(StudentFirstName, StudentAge) values('Larry', 21); Query OK, 1 row affected (0.40 sec) mysql> insert into DemoTable(StudentFirstName, StudentAge) values('Johnny', 23); Query OK, 1 row affected (0.43 sec) mysql> insert into DemoTable(StudentFirstName, StudentAge) ... Read More

How to set default date time as system date time in MySQL?

Samual Sam
Updated on 30-Jul-2019 22:30:25

429 Views

You can use CURRENT_TIMESTAMP to set system date time. Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    ClientFirstName varchar(20),    ClientLastName varchar(20),    ClientAge int ); Query OK, 0 rows affected (0.66 sec)Following is the query to set default datetime as system date time in MySQL −mysql> alter table DemoTable add column ClientProjectDeadline timestamp default current_timestamp; Query OK, 0 rows affected (0.46 sec) Records: 0 Duplicates: 0 Warnings: 0Let us check the description of table once again −mysql> desc DemoTable;This will produce the following output −+-----------------------+-------------+------+-----+-------------------+----------------+ | Field   ... Read More

Counting number of positive and negative votes in MySQL?

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

1K+ Views

To count number of positive and negative votes, you can use CASE statement along with aggregate function SUM().Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Vote int ); Query OK, 0 rows affected (1.70 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Vote) values(-10); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(Vote) values(100); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Vote) values(45); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Vote) values(-6); Query OK, 1 row ... Read More

Advertisements