Found 6702 Articles for Database

Select nth highest value in MySQL

AmitDiwan
Updated on 16-Dec-2019 07:12:56

198 Views

To select the nth highest value in MySQL, following is the syntax −select distinct(yourColumnName) from yourTableName order by yourColumnName DESC limit (NthValue-1), 1;Let us first create a table −mysql> create table DemoTable1594    -> (    -> Marks int    -> ); Query OK, 0 rows affected (0.49 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1594 values(76); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1594 values(95); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1594 values(56); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1594 values(96); Query OK, ... Read More

Alternative to MySQL CASE WHEN in MySQL

AmitDiwan
Updated on 16-Dec-2019 07:04:51

540 Views

Use IF() method as an alternative to CASE WHEN in MySQL. Let us first create a table −mysql> create table DemoTable1593    -> (    -> PlayerScore int    -> ); Query OK, 0 rows affected (0.44 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1593 values(78); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1593 values(0); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1593 values(89); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1593 values(0); Query OK, 1 row affected (0.16 sec)Display all records from the table using ... Read More

Only update the MySQL field if the field contains null or 0?

AmitDiwan
Updated on 16-Dec-2019 06:52:29

138 Views

For this, set conditions using MySQL IF(). Let us first create a table −mysql> create table DemoTable1592    -> (    -> StudentMarks int    -> ); Query OK, 0 rows affected (0.76 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1592 values(56); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1592 values(NULL); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1592 values(98); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1592 values(0); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1592 values(75); Query OK, 1 row affected ... Read More

How to convert varchar “time” to real time in MySQL?

AmitDiwan
Updated on 16-Dec-2019 06:51:20

318 Views

For this, you can use TIME_FORMAT(). Let us first create a table −mysql> create table DemoTable1591    -> (    -> ArrivalTime varchar(20)    -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1591 values('1620'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1591 values('2345'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1591 values('2210'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select * from DemoTable1591;This will produce the following output −+-------------+ | ArrivalTime | +-------------+ ... Read More

Combine two MySQL fields and update a third one with result?

AmitDiwan
Updated on 16-Dec-2019 06:49:03

442 Views

Following is the syntax to combine two fields in MySQL −alter table yourTableName add column yourColumnName dataType;  update yourTableName set yourAddedColumnName =concat(yourColumnName1, ' ', yourColumnName2);Let us first create a table −mysql> create table DemoTable1590    -> (    -> FirstName varchar(20),    -> LastName varchar(20)    -> ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1590 values('Adam', 'Smith'); Query OK, 1 row affected (0.45 sec) mysql> insert into DemoTable1590 values('John', 'Doe'); Query OK, 1 row affected (0.42 sec) mysql> insert into DemoTable1590 values('David', 'Miller'); Query OK, 1 row affected ... Read More

Can we create a database with a numeric name with MySQL?

AmitDiwan
Updated on 16-Dec-2019 06:46:28

327 Views

You cannot create database with numeric name as shown below −mysql> create database 1233;This will produce the following output −ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1233' at line 1To create a database with numeric name, you need to use backticks around the database name −create database yourDatabaseName;Let us implement the above syntax −mysql> create database `1233`; Query OK, 1 row affected (0.20 sec)Now you can switch to the same database −mysql> use `1233`; Database changed

How can I see how long statements take to execute on the MySQL command line?

AmitDiwan
Updated on 16-Dec-2019 06:44:39

102 Views

For every single statement on MySQL command line, it shows the exact time to execute the specific statement.Let us first create a table −mysql> create table DemoTable1589    -> (    -> EmployeeId int,    -> EmployeeName varchar(20)    -> ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1589 values(101, 'Sam'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1589 values(102, 'Bob'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1589 values(103, 'David'); Query OK, 1 row affected (0.16 sec)Display all records from the table ... Read More

Select maximum of sum of two columns in MySQL

AmitDiwan
Updated on 16-Dec-2019 06:42:04

462 Views

To select maximum of sum of two columns, use aggregate function MAX() along with subquery. Let us first create a table −mysql> create table DemoTable1587    -> (    -> Value1 int,    -> Value2 int    -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1587 values(30, 50); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1587 values(80, 90); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1587 values(40, 67); Query OK, 1 row affected (0.13 sec)Display all records from the table using select ... Read More

Display information about field names in MySQL including TYPE, KEY, etc.

AmitDiwan
Updated on 16-Dec-2019 06:39:14

85 Views

To display information about field names, the syntax is as follows −show columns from yourTableName;Let us first create a table −mysql> create table DemoTable1586    -> (    -> EmployeeId int,    -> EmployeeFirstName varchar(20),    -> EmployeeLastName varchar(20),    -> EmployeeAge int,    -> EmployeeCountryName varchar(20),    -> EmployeeSalary int    -> ); Query OK, 0 rows affected (0.78 sec)Following is the query to display field names −mysql> show columns from DemoTable1586;This will produce the following output −+---------------------+-------------+------+-----+---------+-------+ | Field               | Type        | Null | Key | Default | ... Read More

What will happen if we have set UNIQUE and multiple insertion with duplicate values

AmitDiwan
Updated on 16-Dec-2019 06:35:52

87 Views

An error will arise and nothing will get inserted in the table Let us see an example and create a table −mysql> create table DemoTable1585    -> (    -> StudentId int,    -> StudentMarks int,    -> UNIQUE(StudentId)    -> ); Query OK, 0 rows affected (1.02 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1585 values(1,87),(2,98),(3,91),(3,48); ERROR 1062 (23000): Duplicate entry '3' for key 'StudentId'Display all records from the table using select statement −mysql> select * from DemoTable1585;This will produce the following output. Nothing gets inserted:Empty set (0.00 sec)

Advertisements