Found 4378 Articles for MySQL

How do I write not greater than in a MySQL query?

AmitDiwan
Updated on 10-Dec-2019 08:03:33

810 Views

The not greater than in a query can be written simply like less than or equal to ( (    -> StudentName varchar(40),    -> StudentMarks int    -> ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1480 values('Chris', 78); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1480 values('Bob', 45); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1480 values('John', 67); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1480 values('Adam', 56); Query OK, 1 row affected (0.14 sec)Display all records ... Read More

Can we insert records in a MySQL table without auto_increment values?

AmitDiwan
Updated on 10-Dec-2019 08:00:33

1K+ Views

Yes, we can insert without the auto_increment since it gets inserted on its own. Let us first create a table −mysql> create table DemoTable1479    -> (    -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> EmployeeSalary int    -> ); Query OK, 0 rows affected (0.86 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1479(EmployeeSalary) values(6800); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1479(EmployeeSalary) values(5600); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1479(EmployeeSalary) values(5700); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1479(EmployeeSalary) values(6900); Query ... Read More

Count NOT NULL values from separate tables in a single MySQL query

AmitDiwan
Updated on 10-Dec-2019 07:58:11

128 Views

To count values from separate tables, the syntax is as follows −Select    (    select count(yourColumnName) from yourTableName1) as anyAliasName1,    (    select count(yourColumnName) from yourTableName2) as anyAliasName2;Let us first create a table −mysql> create table DemoTable1    -> (    -> Id int    -> ); Query OK, 0 rows affected (1.06 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(1); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1 values(NULL); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1 values(2); Query OK, 1 row affected (0.34 sec) ... Read More

Reorder keys after deleting a record from MySQL table?

AmitDiwan
Updated on 10-Dec-2019 07:53:47

406 Views

For this, use UPDATE command with some mathematical calculations. To delete an id, use the DELETE. Let us first create a table −mysql> create table DemoTable1476    -> (    -> Id int    -> ); Query OK, 0 rows affected (0.81 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1476 values(10); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1476 values(20); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1476 values(30); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1476 values(40); Query OK, 1 row affected (0.12 sec)Display all ... Read More

A single MySQL select query on two tables is possible?

AmitDiwan
Updated on 10-Dec-2019 07:49:57

190 Views

Yes, it is possible. Following is the syntax −select * from yourTableName1, yourTableName2;Let us first create a table −mysql> create table DemoTable1    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY    -> ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(), (), (); Query OK, 3 rows affected (0.14 sec) Records: 3  Duplicates: 0  Warnings: 0Display all records from the table using select statement −mysql> select * from DemoTable1;This will produce the following output −+----+ | Id | +----+ |  1 | |  2 ... Read More

Fetch a value between different values in MySQL

AmitDiwan
Updated on 10-Dec-2019 07:45:53

119 Views

Use MySQL BETWEEN to fetch a value between different values. Let us first create a table −mysql> create table DemoTable1473    -> (    -> EmployeeCode varchar(20)    -> ); Query OK, 0 rows affected (0.47 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1473 values('EMP_120'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable1473 values('EMP_125'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1473 values('EMP_30'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1473 values('EMP_130'); Query OK, 1 row affected (0.10 sec)Display all records from the table using select ... Read More

How to convert char field to datetime field in MySQL?

AmitDiwan
Updated on 10-Dec-2019 07:42:39

251 Views

Let us first create a table. Here, we have declared dates in char type −mysql> create table DemoTable1472    -> (    -> ShippingDate char(35)    -> ); Query OK, 0 rows affected (0.46 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1472 values('12/31/2017 10:50'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1472 values('01/10/2018 12:00'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1472 values('03/20/2019 09:30'); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> select * from DemoTable1472;This will produce the following output ... Read More

MySQL query to calculate the days between two dates from different columns but similar rows

AmitDiwan
Updated on 10-Dec-2019 07:39:53

332 Views

Let us first create a table −mysql> create table DemoTable1471    -> (    -> EmployeeJoiningDate date,    -> EmployeeRelievingDate date    -> ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1471 values('2018-06-21', '2018-12-21'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1471 values('2017-01-19', '2019-01-31'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1471 values('2015-12-31', '2016-03-01'); Query OK, 1 row affected (0.11 sec)Display all records from the table using select statement −mysql> select * from DemoTable1471;This will produce the following output −+---------------------+-----------------------+ | EmployeeJoiningDate | ... Read More

Select a field and if it's null, select another with MySQL?

AmitDiwan
Updated on 10-Dec-2019 07:38:05

83 Views

For this, use COALESCE(). Let us first create a table −mysql> create table DemoTable1470    -> (    -> FirstName varchar(20),    -> Age int    -> ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1470 values('Robert', 23); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1470 values('Bob', NULL); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1470 values(NULL, 25); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement −mysql> select * from DemoTable1470;This will produce the following output ... Read More

Remove index from a MySQL table

AmitDiwan
Updated on 10-Dec-2019 07:34:41

436 Views

To remove index from a MySQL table, the syntax is as follows −alter table yourTableName drop index `yourIndexName`;Let us first create a table −Mysql> create table DemoTable1469    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(40),    -> StudentAge int    -> ); Query OK, 0 rows affected (0.78 sec)Following is the query to add index on column name −mysql> create index `Student Name_Index` on DemoTable1469(StudentName); Query OK, 0 rows affected (0.33 sec) Records: 0  Duplicates: 0  Warnings: 0Let us check the table description −mysql> desc DemoTable1469;This will produce the following output −+-------------+-------------+------+-----+---------+----------------+ ... Read More

Advertisements