Found 4378 Articles for MySQL

MySQL query to select bottom n records

AmitDiwan
Updated on 22-Aug-2019 07:29:07

813 Views

Let us first create a table −mysql> create table DemoTable724 (Value int); Query OK, 0 rows affected (0.79 sec)Insert some records in the table using insert command −mysql> insert into DemoTable724 values(101); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable724 values(183); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable724 values(983); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable724 values(234); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable724 values(755); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable724 values(435); Query OK, 1 row affected (0.28 sec) mysql> ... Read More

Select a value from MySQL database only if it exists only once from a column with duplicate and non-duplicate values

AmitDiwan
Updated on 22-Aug-2019 07:27:37

371 Views

Let us first create a table −mysql> create table DemoTable723 (Value int); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable723 values(100); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable723 values(200); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable723 values(200); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable723 values(100); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable723 values(300); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable723 values(400); Query OK, 1 row affected (0.16 sec) mysql> ... Read More

Replace date format with MySQL STR_TO_DATE

AmitDiwan
Updated on 22-Aug-2019 07:33:37

316 Views

Let us first create a table −mysql> create table DemoTable(DueDate varchar(100)); Query OK, 0 rows affected (0.78 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('10-01-2019 10:19:20'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('12-03-2018 11:00:00'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('02-23-2018 04:20:40'); Query OK, 1 row affected (0.19 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+---------------------+ | DueDate             | +---------------------+ | 10-01-2019 10:19:20 | | 12-03-2018 11:00:00 ... Read More

Is there a way to list all the reserved words in MySQL using the MySQL command-line utility?

AmitDiwan
Updated on 22-Aug-2019 07:26:07

150 Views

Yes, we can all the reserved words in MySQL using the command-line utility. Following is the syntax −select *from mysql.help_keyword;Let us implement the above syntax in order to get list of all the reserved words in MySQL.Following is the query −mysql> select *from mysql.help_keyword;This will produce the following output -

DELETE from MySQL table WHERE column equals value AND column2 equals value2?

AmitDiwan
Updated on 22-Aug-2019 07:24:46

237 Views

Let us first create a table −mysql> create table DemoTable722 (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(100),    StudentAge int ); Query OK, 0 rows affected (1.84 sec)Insert some records in the table using insert command −mysql> insert into DemoTable722(StudentName, StudentAge) values('Chris Brown', 23) ; Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable722(StudentName, StudentAge) values('John Smith', 21); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable722(StudentName, StudentAge) values('David Miller', 22); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable722(StudentName, StudentAge) values('Adam Smith', 20); Query OK, 1 row affected ... Read More

Is there such thing as a SELECT with an IF/ELSE statement in MySQL?

AmitDiwan
Updated on 22-Aug-2019 07:23:05

102 Views

Yes, SELECT with an IF/ELSE is possible in MySQL.Let us first create a table −mysql> create table DemoTable721 (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(100),    Marks int,    CountryName varchar(100) ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable721(FirstName, Marks, CountryName) values('Chris', 56, 'US'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable721(FirstName, Marks, CountryName) values('Robert', 78, 'UK'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable721(FirstName, Marks, CountryName) values('Mike', 45, 'AUS'); Query OK, 1 row affected (0.16 sec) ... Read More

REGEX in MySQL to display only numbers separated by hyphen.

AmitDiwan
Updated on 22-Aug-2019 07:26:32

314 Views

Let us first create a table −mysql> create table DemoTable    (       Code varchar(100)    ); Query OK, 0 rows affected (1.16 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('100-677-9876'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('100-677-9876-John'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('David-100-677-9876'); Query OK, 1 row affected (0.16 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+--------------------+ | Code             | +--------------------+ ... Read More

I used ‘from’ and ‘to’ with backticks as column titles in my database table. Now, how do I SELECT them?

AmitDiwan
Updated on 22-Aug-2019 07:21:14

87 Views

At first, to use reserved words, it should be set with backticks like −`from` `to`Since you want to select the column names set with backtick as shown above, you need to implement the following −select `to`, `from` from yourTableName;Let us first create a table with column names as from and to with backticks −mysql> create table DemoTable720 (    `from` date,    `to` date ); Query OK, 0 rows affected (0.69 sec)Insert some records in the table using insert command −mysql> insert into DemoTable720 values('2019-01-21', '2019-07-23'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable720 values('2017-11-01', '2018-01-31'); Query ... Read More

Prevent having a zero value in a MySQL field?

AmitDiwan
Updated on 22-Aug-2019 07:22:24

477 Views

Use trigger BEFORE INSERT on the table to prevent having a zero value in a MySQL field. Let us first create a table −mysql> create table DemoTable(Value int); Query OK, 0 rows affected (0.85 sec)Let us create a trigger to prevent having a zero value in a MySQL field −mysql> DELIMITER // mysql> create trigger preventing_to_insert_zero_value    before insert on DemoTable    for each row    begin       if(new.Value = 0) then SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'You can not provide 0 value';    END if; end // Query OK, 0 rows affected (0.34 sec) mysql> DELIMITER ... Read More

What does the slash mean in a MySQL query?

AmitDiwan
Updated on 22-Aug-2019 07:19:47

557 Views

The slash means division ( /) in MySQL query. This can be used to divide two numbers. Here, we will see an example to divide numbers from two columns and display result in a new column.Let us first create a table −mysql> create table DemoTable719 (    FirstNumber int,    SecondNumber int ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable719 values(20, 10); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable719 values(500, 50); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable719 values(400, 20); ... Read More

Advertisements