Found 4219 Articles for MySQLi

Select the maximum for each value in a MySQL table?

AmitDiwan
Updated on 18-Dec-2019 05:03:19

141 Views

For this, use GROUP BY clause along with MAX(). Let us first create a table −mysql> create table DemoTable    -> (    -> CountryName varchar(20),    -> Population int    -> ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('US', 560); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values('UK', 10090); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('UK', 8794); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('US', 1090); Query OK, 1 row affected (0.21 sec)Display ... Read More

Does grant on *.* apply to databases created after the grant in MySQL?

AmitDiwan
Updated on 18-Dec-2019 05:00:47

95 Views

Yes, since this is a global privilege. Let us first create a user −mysql> CREATE USER 'Jace'@'localhost' IDENTIFIED BY 'Jace123'; Query OK, 0 rows affected (0.67 sec)Here is the query to grant for global privileges with *.*:mysql> GRANT SELECT ON *.* TO 'Jace'@'localhost'; Query OK, 0 rows affected (0.58 sec)Now you can show all grants for a user −mysql> show grants for 'Jace'@'localhost';This will produce the following output −+-------------------------------------------+ | Grants for Jace@localhost                 | +-------------------------------------------+ | GRANT SELECT ON *.* TO `Jace`@`localhost` | +-------------------------------------------+ 1 row in set (0.14 sec)

MySQL query to sort by both timestamp and enum?

AmitDiwan
Updated on 17-Dec-2019 08:06:34

74 Views

For this, you can use ORDER BY DATE(). Let us first create a table. Here, we have a column with type DATE and another with type ENUM −mysql> create table DemoTable    -> (    -> JoiningDate date,    -> Status ENUM('Good', 'Excellent', 'Bad')    -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-21', 'Excellent'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(Status) values('Bad'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(Status) values('Good'); Query OK, 1 row affected (0.13 sec)Display all ... Read More

Sort records with special characters like '2321/78/54-6'

AmitDiwan
Updated on 26-Feb-2020 06:01:18

97 Views

To sort, use ORDER BY SUBSTRING(). Let us first create a table −mysql> create table DemoTable    -> (    -> Value varchar(40)    -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2321/78/54-6')    -> ; Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2321/78/54-8'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('2321/78/54-5'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('2321/78/54-9'); Query OK, 1 row affected (0.21 sec)Display all records from the table using select statement ... Read More

Display only date from timestamp value in MySQL

AmitDiwan
Updated on 17-Dec-2019 08:01:13

174 Views

To display the only date from timestamp value, use the FROM_UNIXTIME() method in MySQL. Let us first create a table −mysql> create table DemoTable    -> (    -> timestampValue bigint    -> ); Query OK, 0 rows affected (0.70 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(1538332200); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(1577730600); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(1488652200); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the ... Read More

How to use a select statement while updating in MySQL?

AmitDiwan
Updated on 17-Dec-2019 07:58:32

228 Views

For this, use sub query along with WHERE clause while using the MySQL UPDATE command. Let us first create a table −mysql> create table DemoTable    -> (    -> Id int,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.82 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 'Chris'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(250, 'David'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable values(150, 'Mike'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select ... Read More

Query results that have less than X characters in MySQL?

AmitDiwan
Updated on 17-Dec-2019 07:54:29

120 Views

You can use CHAR_LENGTH() along with the WHERE clause. Let us first create a table −mysql> create table DemoTable    -> (    -> FullName varchar(50)    -> ); Query OK, 0 rows affected (1.75 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris Brown'); Query OK, 1 row affected (0.40 sec) mysql> insert into DemoTable values('David Miller'); Query OK, 1 row affected (0.91 sec) mysql> insert into DemoTable values('Robert Miller'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values('John Smith'); Query OK, 1 row affected (0.89 sec)Display all records from the ... Read More

How to split a column in MySQL?

AmitDiwan
Updated on 17-Dec-2019 07:53:04

2K+ Views

To split a column, you need to use SUBSTRING_INDEX() in MySQL. Let us first create a table −mysql> create table DemoTable    -> (    -> Name varchar(40)    -> ); Query OK, 0 rows affected (1.80 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John_Smith'); Query OK, 1 row affected (0.36 sec) mysql> insert into DemoTable values('Carol_Taylor'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values('David_Miller'); Query OK, 1 row affected (0.54 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+--------------+ ... Read More

Add a single year to all the date records in a MySQL table column

AmitDiwan
Updated on 17-Dec-2019 07:49:36

141 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> JoiningDate datetime    -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2015-01-21'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2017-04-02'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('2018-12-31'); 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 −+---------------------+ | JoiningDate         | +---------------------+ | 2015-01-21 00:00:00 ... Read More

Placing comments in the middle of a create table statement? Is it possible?

AmitDiwan
Updated on 17-Dec-2019 07:44:06

47 Views

Yes, it is possible using the # symbol. Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY , # Creating a sequence Number    -> FirstName varchar(20), # Creating a column to store name    -> Age int # creating a column to store an age    -> ); Query OK, 0 rows affected (1.43 sec)Above, we have set comments using the # symbol. Insert some records in the table using insert command. We will set comments in insert statement as well using the same # symbol ... Read More

Advertisements