Found 4378 Articles for MySQL

Is there a default ORDER BY value in MySQL?

Sharon Christine
Updated on 30-Jun-2020 11:35:40

175 Views

There is no default ORDER BY value in MySQL. You need to specify ORDER BY clause explicitly. Following is the syntax −ORDER BY ASC; OR ORDER BY DESC;Let us first create a table −mysql> create table DemoTable    -> (    -> StudentName varchar(100)    -> ); Query OK, 0 rows affected (0.82 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Sam'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.15 sec) ... Read More

Change a MySQL column to have NOT NULL constraint

Sharon Christine
Updated on 30-Jun-2020 11:45:00

552 Views

To update the constraint, use the MODIFY command. Following is the syntax −alter table yourTableName modify yourExistingColumnName yourExistingDataType NOT NULL;  Let us first create a table −mysql> create table DemoTable    -> (    -> UserId int NOT NULL AUTO_INCREMENT,    -> UserFirstName varchar(100),    -> UserLastName varchar(100),    -> UserEmailId varchar(100),    -> UserPassword varchar(100),    -> PRIMARY KEY(UserId)    -> ); Query OK, 0 rows affected (0.91 sec)Following is the query to change the constraint of a column to NOT NULL −mysql> alter table DemoTable modify UserFirstName varchar(100) NOT NULL; Query OK, 0 rows affected (2.13 sec) Records: ... Read More

MySQL DATE_ADD() to increment a date based on the value in another column?

Sharon Christine
Updated on 30-Jun-2020 11:47:25

599 Views

Let us first create a table with one of the columns as DueDate and another one “RepeatTime, which displays how many times, let’s say a user was reminded to submit the payment −mysql> create table DemoTable    -> (    -> DueDate date,    -> RepeatTime int    -> ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-23', 3); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('2019-06-22', 6); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('2019-03-28', 2); Query ... Read More

MySQL query to order by two fields and NULL values in chronological order?

Sharon Christine
Updated on 30-Jun-2020 11:51:42

198 Views

Let us first create a table −mysql> create table DemoTable -> ( -> FirstName varchar(100), -> LastName varchar(100) -> ); Query OK, 0 rows affected (1.39 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Sam', 'Brown'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable values(null, 'Smith'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('David', 'Taylor'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('Mike', null); Query OK, 1 row affected (0.45 sec)Display all records from the table using select statement −mysql> select ... Read More

MySQL query to search record with apostrophe?

Sharon Christine
Updated on 30-Jun-2020 11:13:57

398 Views

Let us first create a table −mysql> create table DemoTable -> ( -> Name varchar(100) -> ); Query OK, 0 rows affected (2.23 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('Sam\''s'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values('David\''s'); Query OK, 1 row affected (0.36 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+----------+ | Name | +----------+ | John ... Read More

MySQL query to fecth the domain name from email-id?

karthikeya Boyini
Updated on 30-Jun-2020 11:15:33

565 Views

Use SUBSTRING_INDEX() for this. Let us first create a table −mysql> create table DemoTable -> ( -> UserMailId varchar(100) -> ); Query OK, 0 rows affected (0.68 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John@gmail.com'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('Carol94844@yahoo.com'); Query OK, 1 row affected (0.25 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+----------------------+ | UserMailId | +----------------------+ | John@gmail.com ... Read More

Selecting records within a range and with a condition set on two columns in MySQL?

karthikeya Boyini
Updated on 30-Jun-2020 11:16:59

369 Views

For this, use where clause. Let us first create a table −mysql> create table DemoTable -> ( -> Number1 int, -> Number2 int -> ); Query OK, 0 rows affected (3.73 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(40, 50); Query OK, 1 row affected (0.60 sec) mysql> insert into DemoTable values(100, 59); Query OK, 1 row affected (0.56 sec) mysql> insert into DemoTable values(400, 500); Query OK, 1 row affected (0.40 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+---------+---------+ | ... Read More

Truncate results in decimal to integer value with MySQL

karthikeya Boyini
Updated on 30-Jun-2020 11:18:07

428 Views

Use truncate() for this, for example, 190.245 to 190. Following is the syntax −select truncate(yourColumnName, 0) from yourTableName;Let us first create a table −mysql> create table DemoTable    -> (    -> Value DECIMAL(10, 4)    -> ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(45.567); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(100.0000); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(15.89000); Query OK, 1 row affected (0.25 sec)Display all records from the table using select statement −mysql> ... Read More

Get the maximum count of distinct values in a separate column with MySQL

karthikeya Boyini
Updated on 30-Jun-2020 11:19:22

346 Views

Use COUNT() function along with GROUP BY clause for this. Let us first create a table −mysql> create table DemoTable -> ( -> Name varchar(100) -> ); Query OK, 0 rows affected (0.69 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Sam'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Mike'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('John'); Query ... Read More

Update column data without using temporary tables in MySQL?

Sharon Christine
Updated on 30-Jun-2020 11:20:32

251 Views

For this, use CASE statement. This will work even without using temporary tables. Let us first create a table −mysql> create table DemoTable    -> (    -> UserName varchar(100),    -> UserStatus varchar(100)    -> ); Query OK, 0 rows affected (0.74 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John', 'Active'); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable values('Chris', 'Inactive'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Bob', 'Inactive'); Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable values('Robert', 'Active'); ... Read More

Advertisements