Found 4219 Articles for MySQLi

How to display grant defined for a MySQL user?

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

160 Views

Use SHOW GRANTS for this. Following is the syntax −SHOW GRANTS FOR 'yourUserName'@'yourHostName';Let us display the user name and host name from MySQL.user table.mysql> select user, host from MySQL.user;This will produce the following output −+------------------+-----------+ | user | host | +------------------+-----------+ | Bob | % | | Charlie | % ... Read More

Can we return query results in same order as the values in MySQL `IN(…)` statement?

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

130 Views

Yes, you can achieve this with ORDER BY FIELD() from MySQL. Let us first create a table −mysql> create table DemoTable -> ( -> Number int -> ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(19); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(34); Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable values(28); Query OK, 1 row ... Read More

Can we use IN() to search between comma separated values within one field?

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

119 Views

Instead of IN(), use FIND_IN_SET to search between comma separated values within one field. Let us first create a table −mysql> create table DemoTable -> ( -> ListOfValues text -> ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('10|20|30|40|50|60|100'); Query OK, 1 row affected (0.18 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+-----------------------+ | ListOfValues | +-----------------------+ | 10|20|30|40|50|60|100 ... Read More

Get the count of duplicate values from a single column in MySQL?

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

166 Views

Let us first create a table −mysql> create table DemoTable -> ( -> Number int -> ); Query OK, 0 rows affected (0.83 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(10); Query OK, ... Read More

Why does MySQL evaluate “TRUE or TRUE and FALSE” to true?

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

142 Views

MySQL evaluates “TRUE or TRUE and FALSE” to true because AND has the highest priority than OR i.e. AND is evaluated before OR.The MySQL evaluates the above statement like this. The AND operator gets evaluated first −(TRUE or (TRUE AND FALSE))The statement (TRUE AND FALSE) gives the result FALSE. Then the second statement evaluates like this −(TRUE or FALSE)The above statement gives the result TRUE.Let us implement one by one −mysql> select (TRUE AND FALSE); +------------------+ | (TRUE AND FALSE) | +------------------+ | 0 | ... Read More

How can we grant a user to access all stored procedures in MySQL?

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

1K+ Views

Let us first display all users and host from the table MySQL.user −mysql> select user, host from Mysql.user;This will produce the following output −+------------------+-----------+ | user             | host      | +------------------+-----------+ | Bob              | %         | | Charlie          | %         | | Robert           | %         | | User2 | % ... Read More

How to correctly use INSERT INTO … SELECT in MySQL to avoid Error 1064?

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

517 Views

Let us first create a table −mysql> create table DemoTable1    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> FirstName varchar(100)    -> ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1(FirstName) values('John'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1(FirstName) values('Chris'); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select *from DemoTable1;This will produce the following output −+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | John ... Read More

Can we skip a column name while inserting values in MySQL?

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

635 Views

Yes, we can do that. Let us first create a table −mysql> create table DemoTable    -> (    -> StudentName varchar(100),    -> StudentAge int    -> ); Query OK, 0 rows affected (0.72 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentAge) values(23); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(StudentName) values('John'); Query OK, 1 row affected (0.21 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output. NULL will get inserted for the skipped column values −+-------------+------------+ | StudentName | StudentAge ... Read More

How to find a value between range in MySQL?

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

573 Views

For this, use BETWEEN operator in MySQL. Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Start int,    -> End int    -> ); Query OK, 0 rows affected (0.91 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Start, End) values(100, 200); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(Start, End) values(400, 500); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(Start, End) values(210, 350); Query OK, 1 row affected (0.11 sec)Display all ... Read More

Add a new column and index to an existing table with ALTER in a single MySQL query?

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

348 Views

To add a new column to an existing table, use ADD. With that, to add a new index, use the ADD INDEX(). Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> Name varchar(100),    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.69 sec)Let us check the description of the table −mysql> desc DemoTable;This will produce the following output −+-------+--------------+------+-----+---------+----------------+ | Field | Type         | Null | Key | Default | Extra          | +-------+--------------+------+-----+---------+----------------+ | Id ... Read More

Advertisements