Found 4219 Articles for MySQLi

MySQL query to select distinct order by id

karthikeya Boyini
Updated on 30-Jun-2020 13:41:18

845 Views

For this, use ORDER BY MAX(). Let us first create a table −mysql> create table DemoTable    -> (    -> Id int,    -> Name varchar(100)    -> ); Query OK, 0 rows affected (0.81 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10, 'John'); Query OK, 1 row affected (0.51 sec) mysql> insert into DemoTable values(10, 'John'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(20, 'Sam'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(20, 'Sam'); Query OK, 1 row affected (0.11 sec) ... Read More

Comparing two columns in a single MySQL query to get one row?

Sharon Christine
Updated on 30-Jun-2020 13:42:26

573 Views

For this, you can use ORDER BY clause. Let us first create a table −mysql> 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(60, 249); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(59, 250); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+------+------+ | Num1 | Num2 | +------+------+ | 60 | 249 ... Read More

Discard last 3 characters of a field in MySQL

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

341 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> StudentId varchar(100)    -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('STU-090'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('STU-123'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('STU-678'); Query OK, 1 row affected (0.29 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+-----------+ | StudentId | +-----------+ | STU-090 | ... Read More

Get the substring of a column in MySQL

karthikeya Boyini
Updated on 30-Jun-2020 13:44:57

1K+ Views

Use the SUBSTR() method to get the substring of a column.Let us first create a table −mysql> create table DemoTable -> ( -> Title text -> ); Query OK, 0 rows affected (0.74 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('This is a MySQL Database'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Java is an Object Oriented Programming Language'); Query OK, 1 row affected (0.17 sec)Display all records from the table using select statement −mysql> select *from DemoTable ;OutputThis will produce the following output −+-------------------------------------------------+ | Title ... Read More

MySQL query to select date from timestamp?

karthikeya Boyini
Updated on 30-Jun-2020 13:21:07

373 Views

Let us first create a table. One of the column is a timestamp −mysql> create table DemoTable    -> (    -> CustomerName varchar(100),    -> CustomerShippingDate timestamp    -> ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris', '2019-01-21'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('David', '2019-03-01'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('Robert', '2019-06-04'); Query OK, 1 row affected (0.25 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will ... Read More

How do I select 5 random rows from the 20 most recent rows in MySQL?

Sharon Christine
Updated on 30-Jun-2020 13:22:49

395 Views

For random, use RAND() method. And for limit on rows, use the LIMIT() method.Let us first create a table −mysql> create table DemoTable    -> (    -> ShippingDate datetime    -> ); Query OK, 0 rows affected (0.51 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-01'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('2019-01-03'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('2019-01-05'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('2019-01-07'); Query OK, 1 row affected (0.11 sec) ... Read More

Remove all except the first character of a string in MySQL?

Sharon Christine
Updated on 30-Jun-2020 13:24:00

214 Views

For this, use the LEFT() method, which returns a specified number of characters from the left of the string.Let us first create a table −mysql> create table DemoTable    -> (    -> FirstName varchar(100)    -> ); Query OK, 0 rows affected (0.96 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Sam'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Mike'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> ... Read More

MySQL query with two boolean conditions to extract date based on hour?

Sharon Christine
Updated on 30-Jun-2020 13:24:50

105 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> AdmissionDate datetime    -> ); Query OK, 0 rows affected (0.75 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-10 10:45:10'); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable values('2019-02-12 20:50:00'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('2019-02-12 16:10:19'); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+---------------------+ | AdmissionDate ... Read More

How to remove all instances of a specific character from a column in MySQL?

Kumar Varma
Updated on 30-Jun-2020 13:26:05

834 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> FirstName varchar(100)    -> ); Query OK, 0 rows affected (0.41 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Adam^^^'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('^^^^^^^^Carol'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('Robert^^^^^^'); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+---------------+ | FirstName     | +---------------+ | Adam^^^   ... Read More

How to create and fill a new column in an already created MySQL table?

Rama Giri
Updated on 30-Jun-2020 13:27:16

1K+ Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Value1 int,    -> Value2 int    -> ); Query OK, 0 rows affected (0.77 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10, 10); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(20, 30); Query OK, 1 row affected (0.24 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+--------+--------+ | Value1 | Value2 | +--------+--------+ |     10 |   10 | ... Read More

Advertisements