Found 4219 Articles for MySQLi

How to sort time in AM/ PM in MySQL?

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

2K+ Views

To sort time in AM/PM in MySQL, you can use ORDER BY STR_TO_DATE(). Following is the syntax −select yourColumnName from yourTableName ORDER BY STR_TO_DATE(yourColumnName , '%l:%i %p');Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    UserLogoutTime varchar(200) ); Query OK, 0 rows affected (0.97 sec)Insert records in the table using insert command −mysql> insert into DemoTable(UserLogoutTime) values('09:45 PM'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(UserLogoutTime) values('11:56 AM'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(UserLogoutTime) values('01:01 AM'); Query OK, 1 row affected (0.17 ... Read More

How to create Tab Delimited Select statement in MySQL?

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

690 Views

To create a tab delimited select statement, you can use CONCAT() function from MySQL. Following is the syntax:select concat(yourColumnName1, "\t", yourColumnName2) AS anyAliasName from yourTableName;Let us first create a table:mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(20),    LastName varchar(20) ); Query OK, 0 rows affected (0.81 sec)Following is the query to insert records in the table using insert command:mysql> insert into DemoTable(FirstName, LastName) values('John', 'Smith'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(FirstName, LastName) values('Carol', 'Taylor'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable(FirstName, LastName) ... Read More

How to add separator to numbers using MySQL views?

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

138 Views

Let us first create a table −mysql> create table DemoTable (    StudentId int ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(343898456); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(222333444); Query OK, 1 row affected (0.22 sec)Following is the query to display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+-----------+ | StudentId | +-----------+ | 343898456 | | 222333444 | +-----------+ 2 rows in set (0.00 sec)Here is the query to create view ... Read More

How to get the difference between two columns in a new column in MySQL?

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

6K+ Views

Let us first create a table with columns for which we will calculate the difference in a new column −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    LowValue int,    HighValue int ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(LowValue, HighValue) values(100, 200); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable(LowValue, HighValue) values(300, 700); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(LowValue, HighValue) values(1000, 2000); Query OK, 1 row affected (0.13 sec)Following is the query to ... Read More

How to perform custom sort by field value in MySQL?

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

97 Views

To perform custom sort by field value in MySQL, use the FIELD() method in ORDER BY. Let us first create a table:mysql> create table DemoTable (StudentId int); Query OK, 0 rows affected (0.58 sec)Following is the query to insert some records in the table using insert command:mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(110); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(90); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(70); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(120); Query ... Read More

Can we create a table with a space in name in MySQL?

Nancy Den
Updated on 30-Jul-2019 22:30:25

3K+ Views

To create a table with a space in the table name in MySQL, you must use backticks otherwise you will get an error.Let us first see what error will arise by creating a table with a space in the name i.e. “Demo Table” table name below:mysql> create table Demo Table (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    EmployeeFirstName varchar(20),    EmployeeLastName varchar(20),    EmployeeAge int,    EmployeeSalary int,    EmployeeAddress varchar(200) ); ERROR 1064 (42000): You have an error in your syntax; check the manual that corresponds to your MySQL server version for the right syntax to ... Read More

Converting date from 'dd/mm/yyyy' to 'yyyymmdd' in MySQL

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

1K+ Views

To convert date from 'dd/mm/yyyy' to 'yyyymmdd', you can use the date_format() method. Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Admissiondate varchar(200) ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Admissiondate) values('21/10/2014'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(Admissiondate) values('01/12/2016'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Admissiondate) values('31/01/2017'); Query OK, 1 row affected (0.14 sec)Following is the query to display all records from the table using select statement ... Read More

MySQL SELECT to add a new column to a query and give it a value?

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

12K+ Views

To add column to MySQL query and give it a value, use the below syntax −select yourColumnName1, yourColumnName2, .....N ,yourValue AS anyAliasName from yourTableName;Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(20) ); Query OK, 0 rows affected (0.84 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(FirstName) values('John'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(FirstName) values('Larry'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(FirstName) values('Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into ... Read More

Extract the user ID from the username only in MySQL?

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

1K+ Views

To extract the User ID only from MySQL, you can use SUBSTRING_INDEX(), which extracts the part of a string from the Username to get the User ID. Let us first display the user −mysql> SELECT USER();This will produce the following output −+----------------+ | USER() | +----------------+ | root@localhost | +----------------+ 1 row in set (0.00 sec)Let us now extract the UserID only −mysql> SELECT SUBSTRING_INDEX(USER(), '@', 1);This will produce the following output −+-------------------------------+ | SUBSTRING_INDEX(USER(), '@', 1) | +-------------------------------+ | root ... Read More

How to change a specific char in a MySQL string?

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

242 Views

To change a specific char in a MySQL string, you can use CONCAT() along with SUBSTRING().Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Value varchar(200) ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Value) values('98764'); Query OK, 1 row affected (0.17 sec)Following is the query to display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+----+-------+ | Id | Value | +----+-------+ | 1  | 98764 | +----+-------+ ... Read More

Advertisements