Found 4219 Articles for MySQLi

Find out the records of students with more than a specific score in MySQL?

Naveen Singh
Updated on 30-Jun-2020 10:07:06

98 Views

Set it with WHERE and get the records of students more than a specific score. Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(100),    -> StudentScore int    -> ); Query OK, 0 rows affected (1.65 secInsert some records in the table using insert command −mysql> insert into DemoTable(StudentName, StudentScore) values('John', 43); Query OK, 1 row affected (0.81 sec) mysql> insert into DemoTable(StudentName, StudentScore) values('Sam', 48); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(StudentName, StudentScore) values('Chris', 33); ... Read More

MySQL query to find all rows where string contains less than four characters?

Naveen Singh
Updated on 30-Jul-2019 22:30:26

269 Views

Use CHAR_LENGTH() and find the count of characters in every string and then get the strings which are less than four characters. Let us first create a table −mysql> create table DemoTable -> ( -> Name varchar(100) -> ); Query OK, 0 rows affected (1.38 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('Bob'); Query OK, 1 row affected (1.60 sec) mysql> insert into DemoTable values('Carol'); Query OK, 1 row affected (0.25 ... Read More

Implement MySQL ORDER BY without using ASC or DESC?

Naveen Singh
Updated on 30-Jul-2019 22:30:26

98 Views

For this, you can use FIND_IN_SET(). Let us first create a table −mysql> create table DemoTable -> ( -> Number int -> ); Query OK, 0 rows affected (2.25 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.91 sec) mysql> insert into DemoTable values(60); Query OK, 1 row affected (0.69 sec) mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.72 sec) mysql> insert into DemoTable values(800); Query OK, 1 row affected (0.75 sec)Display all records ... Read More

Best data type for storing large strings in MySQL?

Naveen Singh
Updated on 30-Jul-2019 22:30:26

746 Views

You can use text data type to store large strings. Following is the syntax −CREATE TABLE yourTableName (    yourColumnName text,    .    .    N );Let us first create a table −mysql> create table DemoTable -> ( -> MyStringValue text -> ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('This is a text data type to store large string'); Query OK, 1 row affected (0.17 sec)Display all records from the table using select statement −mysql> select *from ... Read More

How to extract the area codes from a phone number with MySQL?

Naveen Singh
Updated on 30-Jul-2019 22:30:26

896 Views

Let’s say we have a list of phone numbers and from that we want to get the area codes. These area codes are for example, the first 3 digits of the phone number. Use LEFT() function from MySQL for this.Let us first create a table −mysql> create table DemoTable -> ( -> AreaCodes varchar(100) -> ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command. Here, let’s say we have included the phone numbers −mysql> insert into DemoTable values('90387568976') ; Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('90389097878' ; ... Read More

Can we implement 'LIKE' and ‘IN’ in a single MySQL query?

Naveen Singh
Updated on 30-Jul-2019 22:30:26

264 Views

For more efficiency, use Regular Expression for the same task. Let us first create a table −mysql> create table DemoTable -> ( -> Name varchar(30) -> ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected ... Read More

Finding the minimum and maximum value from a string with numbers separated by hyphen in MySQL?

Naveen Singh
Updated on 30-Jul-2019 22:30:26

466 Views

Use MIN() function along with SUBSTRING() for minimum, whereas MAX() for maximum. Let us first create a table −mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Value varchar(100) -> ); Query OK, 0 rows affected (0.76 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Value) values('10-20'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(Value) values('200-100'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Value) values('780-235'); Query OK, 1 row affected ... Read More

How to add NOT NULL constraint to an already created MySQL column?

Naveen Singh
Updated on 30-Jul-2019 22:30:26

251 Views

Achieve this using ALTER TABLE. Let us first create a table −mysql> create table DemoTable    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(100)    -> ); Query OK, 0 rows affected (0.86 sec)Let us check the description of the table −mysql> desc DemoTable;This will produce the following output −+-------------+--------------+------+-----+---------+----------------+ | Field       | Type         | Null | Key | Default | Extra          | +-------------+--------------+------+-----+---------+----------------+ | StudentId   | int(11)      | NO   | PRI | NULL    | auto_increment | ... Read More

How to cut part of a string with a MySQL query?

Naveen Singh
Updated on 30-Jul-2019 22:30:26

324 Views

For this, use substring_index() function from MySQL. Let us first create a table −mysql> create table DemoTable -> ( -> StudentId varchar(100) -> ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('STU-1011'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('STU-95968686'); Query OK, 1 row affected (0.17 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+--------------+ | StudentId | +--------------+ | STU-1011 ... Read More

Split a column after hyphen in MySQL and display the remaining value?

Naveen Singh
Updated on 30-Jul-2019 22:30:26

547 Views

To split a column after hyphen, use the SUBSTRING_INDEX() method −select substring_index(yourColumnName, '-', -1) AS anyAliasName from yourTableName;Let us first create a table −mysql> create table DemoTable -> ( -> StreetName text -> ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Paris Hill St.-CA-83745646') ; Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable values('502 South Armstrong Street-9948443'); Query OK, 1 row affected (0.20 sec)Display all records from the table using select statement −mysql> select *from ... Read More

Advertisements