Found 4219 Articles for MySQLi

How to insert records with double quotes in MySQL?

AmitDiwan
Updated on 12-Dec-2019 05:20:26

3K+ Views

To insert records with double quotes, use the backslash (\) as in the below syntax −Syntaxinsert into yourTableName values('\"yourValue\"');Let us first create a table −mysql> create table DemoTable    -> (    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('\"John\"'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('\"Chris\"'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('\"Adam Smith\"'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('\"Carol\"'); Query OK, 1 row affected ... Read More

How to display only hour and minutes in MySQL?

AmitDiwan
Updated on 12-Dec-2019 05:17:29

3K+ Views

To display only hour and minutes, use DATE_FORMAT() and set format specifiers as in the below syntax −select date_format(yourColumnName, '%H:%i') as anyAliasName from yourTableName;Let us first create a table −mysql> create table DemoTable1527    -> (    -> ArrivalDatetime datetime    -> ); Query OK, 0 rows affected (0.76 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1527 values('2019-01-10 12:34:45'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1527 values('2018-12-12 11:00:34'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1527 values('2019-03-21 04:55:56'); Query OK, 1 row affected (0.18 sec)Display all records from ... Read More

Order by last 3 months first, then alphabetically in MySQL?

AmitDiwan
Updated on 12-Dec-2019 05:14:51

279 Views

Let us first create a table −mysql> create table DemoTable1526    -> (    -> CustomerName varchar(20),    -> PurchaseDate date    -> ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command. Here, we have inserted 2019 dates −mysql> insert into DemoTable1526 values('Adam', '2019-06-01'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1526 values('Sam', '2019-04-26'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1526 values('Chris', '2019-05-24'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1526 values('David', '2019-10-10'); Query OK, 1 row affected (0.23 sec) mysql> insert into ... Read More

Select date from MySQL and format to text?

AmitDiwan
Updated on 11-Dec-2019 11:42:55

169 Views

To select date and format, use SELECT DATE_FORMAT(). Following is the syntax −Syntaxselect date_format(yourColumnName, '%e %b %y') from yourTableName;Let us first create a table −mysql> create table DemoTable    -> (    -> DueDate date    -> ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-11'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('2019-12-21'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('2019-09-15'); Query OK, 1 row affected (0.07 sec)Display all records from the table using select statement −mysql> select *from ... Read More

Group MySQL rows in an array by column value?

AmitDiwan
Updated on 11-Dec-2019 11:40:22

3K+ Views

To group rows in an array, use GROUP_CONCAT() along with the ORDER BY clause. Let us first create a table −mysql> create table DemoTable    -> (    -> Id int,    -> FirstName varchar(20)    -> ); Query OK, 0 rows affected (0.78 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(101, 'John'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(102, 'Bob'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(101, 'David'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(101, 'Adam'); Query OK, ... Read More

Getting last value in MySQL group concat?

AmitDiwan
Updated on 11-Dec-2019 06:39:32

542 Views

To get last value in group concat, use SUBSTRING_INDEX(). Let us first create a table −mysql> create table DemoTable1525    -> (    -> ListOfSubjects text    -> ); Query OK, 0 rows affected (1.13 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1525 values('MongoDB, C'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1525 values('Java, C++, MySQL'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable1525 values('Python, C++, C, Java'); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> select * from DemoTable1525;This will ... Read More

Count the number of columns in a MySQL table with Java

AmitDiwan
Updated on 11-Dec-2019 06:37:17

290 Views

For this, use ResultSetMetaData. Let us first create a table −mysql> create table DemoTable    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentFirstName varchar(20),    -> StudentLastName varchar(20)    -> ); Query OK, 0 rows affected (0.58 sec)The Java code is as follows −Exampleimport java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import com.mysql.jdbc.ResultSetMetaData; public class ResultSetDemo {    public static void main(String[] args) {       Connection con = null;       PreparedStatement ps = null;       Statement st = null;       ResultSet rs = null; ... Read More

Match the elements of an array in a MySQL query

AmitDiwan
Updated on 11-Dec-2019 06:33:22

2K+ Views

Let us first create a table table −mysql> create table DemoTable1523    -> (    -> Id int,    -> Value int    -> ); Query OK, 0 rows affected (0.76 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1523 values(1, 56); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1523 values(2, 78); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1523 values(1, 34); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1523 values(2, 45); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1523 values(1, 99); Query OK, ... Read More

Sum columns corresponding values according to similar dates in MySQL?

AmitDiwan
Updated on 11-Dec-2019 06:31:06

127 Views

For this, use aggregate function SUM() along with GROUP BY. Let us first create a table −mysql> create table DemoTable1522    -> (    -> ProductPurchaseDate date,    -> NumberOfProduct int    -> ); Query OK, 0 rows affected (1.51 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1522 values('2019-01-21', 45); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1522 values('2018-12-31', 78); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1522 values('2019-01-21', 67); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1522 values('2019-03-01', 56); Query OK, 1 row affected (0.19 ... Read More

Update a column in MySQL and remove the trailing underscore values

AmitDiwan
Updated on 11-Dec-2019 06:27:28

417 Views

To remove the trailing values, use TRIM() as in the below update syntax −update yourTableName set yourColumnName=trim(trailing '_' from yourColumnName);Let us first create a table −mysql> create table DemoTable1521    -> (    -> StudentCode varchar(20)    -> ); Query OK, 0 rows affected (1.33 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1521 values('345_'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1521 values('12345'); Query OK, 1 row affected (0.38 sec) mysql> insert into DemoTable1521 values('9084_'); Query OK, 1 row affected (1.29 sec)Display all records from the table using select statement −mysql> select ... Read More

Advertisements