Found 4378 Articles for MySQL

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

423 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

824 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

How to select data from a table where the table name has blank spaces in MYSQL?

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

783 Views

You need to use backticks around the table name where the table name has blank space. Let us first create a table. Here, we have used backtick −mysql> create table `Demo Table138` (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Price int ); Query OK, 0 rows affected (0.47 sec)Insert records in the table using insert command −mysql> insert into `Demo Table138`(Price) values(450); Query OK, 1 row affected (0.18 sec) mysql> insert into `Demo Table138`(Price) values(499); Query OK, 1 row affected (0.16 sec) mysql> insert into `Demo Table138`(Price) values(199); Query OK, 1 row affected (0.17 sec) mysql> insert into ... Read More

Get the Average of Average in a single MySQL row?

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

191 Views

You can use aggregate function AVG() for this. Let us first create a table −mysql> create table DemoTable (    Value1 int,    Value2 int,    Value3 int ); Query OK, 0 rows affected (0.54 sec)Insert records in the table using insert command −mysql> insert into DemoTable values(10, 20, 30); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable values(13, 15, 18); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(21, 31, 41); Query OK, 1 row affected (0.21 sec)Display records from the table using select command −mysql> select *from DemoTable;This will produce the following ... Read More

Advertisements