Found 4219 Articles for MySQLi

Write a MySQL query to check if field exists and then return the result set?

George John
Updated on 30-Jul-2019 22:30:25

256 Views

To check if field exists and then to return the result set, you can use the below syntax −show columns from yourTableName where field='yourColumnName';Let us first create a table −mysql> create table DemoTable (    UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    UserFirstName varchar(20),    UserLastName varchar(20),    UserAge int,    UserAddress varchar(200),    UserCountryName varchar(20) ); Query OK, 0 rows affected (0.67 sec)Here is the query to check if field exists and then return the result set −mysql> show columns from DemoTable where field='UserCountryName';This will produce the following output −+-----------------+-------------+------+-----+---------+-------+ | Field           | ... Read More

Can we know the last MySQL error?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

207 Views

In order to know the last MySQL error, you can use SHOW command −SHOW ERRORS;Or you can use another syntax −SHOW WARNINGS;Here, we are creating a table that displays an error and then we will find out how to know the last MySQL error. Here, the error occurs because we have intentionally written incorrect create table statement −mysql> create table DemoTable(Id int); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'create table DemoTable(Id int)' at line 1Following is the query to ... Read More

PHP and MYSQL database connection and table creation only once if it does not already exist?

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

813 Views

To create database only once, use the below syntax.CREATE DATABASE IF NOT EXISTS yourDatabaseName;To create table only once, use the below syntax −CREATE TABLE IF NOT EXISTS yourTableName (    yourColumnName yourDatatype,    .    .    .    N );Let us implement both the above syntaxes to create database and table only once if does not already exist −mysql> CREATE DATABASE IF NOT EXISTS login; Query OK, 1 row affected (0.23 sec)Above query creates a database successfully.Following is the query to create a table −mysql> CREATE TABLE IF NOT EXISTS DemoTable (    Id int ); Query OK, 0 rows affected (0.56 sec)Above query creates a table successfully.

How to use IF in stored procedure and select in MySQL?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

498 Views

You can use IF in stored procedure and IF() in select statement as well.IF() in select statementIF() in select statement mysql> select if(0=0, 'Hello MySQL', 'condition is wrong');This will produce the following output −+------------------------------------------------------+ | if('test'='test', 'Hello MySQL', 'condition is wrong') | +------------------------------------------------------+ | Hello MySQL | +------------------------------------------------------+ 1 row in set (0.00 sec)The second case if your condition ... Read More

How to match underscore in a MySQL String?

George John
Updated on 30-Jul-2019 22:30:25

747 Views

To match underscore in a MySQL string, you can use the below syntax −select *from yourTableName where yourColumnName LIKE '%\_%';Let us first create a table −mysql> create table DemoTable (    ClientId varchar(200) ); Query OK, 0 rows affected (0.79 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('CLI_101'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('CLI1110'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('_CLI102'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('CLI103_'); Query OK, 1 row affected (0.12 sec) mysql> insert into ... Read More

What MySQL databases do I have permissions on?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

74 Views

To check this, you can use SHOW command. The syntax is as follows −show grants\GLet us implement the above syntax to display the permissions you have −mysql> SHOW GRANTS\GThis will produce the following output −*************************** 1. row *************************** Grants for root@%: GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, CREATE ROLE, DROP ROLE ON *.* TO `root`@`%` WITH GRANT OPTION *************************** 2. row *************************** Grants for root@%: ... Read More

MySQL order by 0 first and then display the record in descending order?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

308 Views

In order to order by 0 first and then largest, you can use the below syntax −select *from yourTableName order by yourColumnName=0 DESC, yourColumnName DESC;Let us first create a table −mysql> create table DemoTable (    Value int ); Query OK, 0 rows affected (0.65 sec)Insert records in the table using insert command −mysql> insert into DemoTable values(90); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(0); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(0); Query OK, 1 row affected (0.12 ... Read More

MySQL query to select column where value = one or value = two, value = three, etc?

Samual Sam
Updated on 30-Jul-2019 22:30:25

766 Views

Let us first create a table −mysql> create table DemoTable (    UserId int,    UserName varchar(10),    UserAge int ); Query OK, 0 rows affected (0.73 sec)Insert records in the table using insert command −mysql> insert into DemoTable values(101, 'Chris', 23); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(102, 'Robert', 33); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values(103, 'David', 25); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(104, 'Carol', 35); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values(105, 'Bob', 29); Query OK, ... Read More

Select MySQL rows where column contains same data in more than one record?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

424 Views

Use MySQL JOIN to select MySQL rows where column contains same data in more than one record. Let us first create a table −mysql> create table DemoTable (    UserId int,    UserName varchar(20) ); Query OK, 0 rows affected (0.54 sec)Insert records in the table using insert command −mysql> insert into DemoTable values(10, 'John'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(11, 'Sam'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(12, 'Larry'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(13, 'David'); Query OK, 1 row affected (0.17 ... Read More

Querying average row length in MySQL?

Samual Sam
Updated on 30-Jul-2019 22:30:25

971 Views

You can use INFORMATION_SCHEMA.TABLES and AVG_ROW_LENGTH to query average row length in MySQL −SELECT AVG_ROW_LENGTH FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = ‘yourTableName’;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.90 sec)Insert records in the table using insert command −mysql> insert into DemoTable(StudentName) values('John'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(StudentName) values('Larry'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(StudentName) values('Sam'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(StudentName) values('Mike'); Query OK, ... Read More

Advertisements