Found 6702 Articles for Database

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

748 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

310 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

767 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

426 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

How to prevent MySQL double insert (duplicate entry)?

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

826 Views

To prevent duplicate entry, add constraint UNIQUE. Let us first create a table −mysql> create table DemoTable (    Id int,    Name varchar(100) ); Query OK, 0 rows affected (0.79 sec)Here is the query to prevent MySQL double insert using UNIQUE −mysql> alter table DemoTable add constraint id_NameUnKey UNIQUE(Id, Name); Query OK, 0 rows affected (0.82 sec) Records: 0 Duplicates: 0 Warnings: 0Insert records in the table using insert command. When we will try to the same record again, then the “Duplicate entry” error will be visible −mysql> insert into DemoTable values(11, 'John'); Query OK, 1 row affected (0.18 ... Read More

How to align a column right-adjusted in MySQL?

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

2K+ Views

You can use LPAD() from MySQL for this. Let us first create a table −mysql> create table DemoTable (    FullName varchar(100) ); Query OK, 0 rows affected (0.81 sec)Insert records in the table using insert command −mysql> insert into DemoTable values('John Smith'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('David Miller'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('Sam Williams'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('Carol Taylor'); Query OK, 1 row affected (0.47 sec)Display records from the table using select command −mysql> select *from DemoTable;This ... Read More

Select all except the first character in a string in MySQL?

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

852 Views

To select all except the first character in a string, you can use SUBSTR() method. Let us first create a table −mysql> create table DemoTable (    FirstName varchar(20) ); Query OK, 0 rows affected (0.63 sec)Insert records in the table using insert command −mysql> insert into DemoTable values('Larry'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values('Carol'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.14 sec)Display records from the table using select command −mysql> ... Read More

Advertisements