Found 4381 Articles for MySQL

Sort a column in descending order after placing argument in MySQL IN()?

karthikeya Boyini
Updated on 30-Jun-2020 14:01:35

93 Views

For this, use FIELD() function with DESC. Let us first create a table −mysql> create table DemoTable -> ( -> Number int -> ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(50); Query OK, 1 row ... Read More

Can we use ORDER BY NULL in MySQL?

karthikeya Boyini
Updated on 30-Jun-2020 14:02:38

485 Views

Yes, we can do thatNote − Before MySQL 5.7, ORDER BY NULL was useful, but with MySQL 8.0, specifying ORDER BY NULL, for example, at the end to suppress implicit sorting is no longer necessary.Let us first create a table −mysql> create table DemoTable -> ( -> Name varchar(10) -> ); Query OK, 0 rows affected (1.01 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Bob'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('David'); Query OK, 1 ... Read More

MySQL XOR operator with IN clause?

Sharon Christine
Updated on 30-Jun-2020 14:03:58

726 Views

MySQL XOR returns TRUE if one or the other operand (or expression) but not both is TRUE. The IN clause is used to specify a condition with any other MySQL query.Let us first create a tablemysql> create table DemoTable -> ( -> Num1 int, -> Num2 int -> ); Query OK, 0 rows affected (0.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(5, 5); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(6, 6); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(7, 7); Query OK, ... Read More

Is it impossible to add a column in MySQL specifically before another column?

Sharon Christine
Updated on 30-Jun-2020 14:05:36

692 Views

No, you can easily add a column before another column using ALTER.Note − To add a column at a specific position within a table row, use FIRST or AFTER col_name Let us first create a table −mysql> create table DemoTable    -> (    -> Id int,    -> Name varchar(20),    -> CountryName varchar(100)    -> ); Query OK, 0 rows affected (0.67 sec)Let us check all the column names from the table −mysql> show columns from DemoTable;OutputThis will produce the following output −+-------------+--------------+------+-----+---------+-------+ | Field       | Type         | Null | Key ... Read More

Adding integers from a variable to a MySQL column?

Sharon Christine
Updated on 30-Jun-2020 14:06:34

823 Views

To set a variable, use MySQL SET. For adding integers from a variable, use UPDATE and SET as in the below syntax −set @anyVariableName:=yourValue; update yourTableName set yourColumnName=yourColumnName+ @yourVariableName;Let us first create a table −mysql> create table DemoTable -> ( -> Number int -> ); Query OK, 0 rows affected (0.68 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.15 sec) mysql> ... Read More

Why BINARY keyword used with MySQL REGEXP operator?

karthikeya Boyini
Updated on 30-Jun-2020 13:48:06

661 Views

Use the BINARY keyword to force REGEXP to match the string as a binary string. We will see the difference here.Let us first create a table −mysql> create table DemoTable -> ( -> Name varchar(100) -> ); Query OK, 0 rows affected (0.46 sec)Insert some records in the table using insert command. We have names here with different cases −mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('JOHN'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('john'); Query OK, 1 row affected (0.16 sec) mysql> insert ... Read More

MySQL query to display a substring before a special character in a string

karthikeya Boyini
Updated on 30-Jun-2020 13:49:13

388 Views

Use the LOCATE() and SUBSTRING() method for this in MySQL. Let us first create a table −mysql> create table DemoTable    -> (    -> Title text    -> ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Introduction To Java'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Introduction - To MySQL'); Query OK, 1 row affected (0.16 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+-------------------------+ | Title           ... Read More

Convert hex string to number in MySQL?

karthikeya Boyini
Updated on 30-Jun-2020 13:51:10

2K+ Views

Use the CONV() method to convert hex string to number −select CONV(yourColumnName, 16, 10) AS anyAliasName from yourTableName;Let us first create a table −mysql> create table DemoTable -> ( -> HexString varchar(100) -> ); Query OK, 0 rows affected (0.82 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('A'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values('F'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('B'); Query OK, 1 row affected (0.55 sec) mysql> insert into DemoTable values('ABC'); Query OK, 1 row affected (0.11 sec)Display ... Read More

Extract the middle part of column values in MySQL surrounded with hyphens and display in a new column?

Sharon Christine
Updated on 30-Jun-2020 13:52:20

247 Views

Use the SUBSTR() method to extract the middle part of column values surrounded with hyphens, for example, “11-84848-11”.Let us first create a table −mysql> create table DemoTable -> ( -> Number varchar(100), -> Number1 varchar(100) -> ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Number) values('11-84848-11'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(Number) values('22-99999-22'); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+-------------+---------+ | Number ... Read More

How to set a string with hyphen and numbers in MySQL varchar?

Sharon Christine
Updated on 30-Jun-2020 13:53:58

3K+ Views

To set a string with hyphen and numbers, you need to use single quotes. For example, 'Customer-1234-899', 'Customer-9383-901', etc.Let us first create a table −mysql> create table DemoTable -> ( -> CustomerId varchar(100) -> ); Query OK, 0 rows affected (0.70 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Customer-1234-899'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Customer-8373-900'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Customer-9383-901'); Query OK, 1 row affected (0.20 sec)Display all records from the table using select statement −mysql> select *from DemoTableOutputThis ... Read More

Advertisements