Found 6702 Articles for Database

Does Ternary operation exist in MySQL just like C or C++?

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

49 Views

Yes, let us first see the working of ternary operator in C or C++ language.X=(X > 10 && ( X-Y) < 0) ?: X:(X-Y);Here is the demo code in C language. After that we will check in MySQL. The C code is as follows −#include int main() {    int X;    int Y;    int result;    printf("Enter the value for X:");    scanf("%d", &X);    printf("Enter the value for Y:");    scanf("%d", &Y);    result=( X > 1 && (X-Y) < 0) ? X: (X-Y);    printf("The Result is=%d", result);    return 0; }The snapshot of C ... Read More

Set the MySQL primary keys auto increment to be unlimited (or incredibly huge)?

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

429 Views

You can use BIGINT but this is not unlimited but you can use large number of primary keys auto increment using it. The syntax is as follows −yourColumnName BIGINT NOT NULL AUTO_INCREMENT;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table LargeAutoIncrement -> ( -> Id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY -> ); Query OK, 0 rows affected (0.78 sec)Now in this table you can store large number like 9223372036854775807 i.e. for primary key auto increment.Let us insert ... Read More

Compare two tables and return missing ids using MySQL LEFT OUTER JOIN

Samual Sam
Updated on 30-Jun-2020 13:19:35

714 Views

To compare two tables and return missing ids, you need to use MySQL LEFT OUTER JOIN.Let us create a table with sample fields and then we will insert records. The query to create the first table −First_Tablemysql> create table First_Table    -> (    -> Id int    -> ); Query OK, 0 rows affected (0.88 sec)Now insert some records in the table using insert command. The query is as follows −mysql> insert into First_Table values(1); Query OK, 1 row affected (0.68 sec) mysql> insert into First_Table values(2); Query OK, 1 row affected (0.29 sec) mysql> insert into First_Table values(3); ... Read More

Searching BETWEEN dates stored as varchar in MySQL?

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

349 Views

You can search between dates stored as varchar using STR_TO_DATE(). The syntax is as follows −select *from yourTableName where STR_TO_DATE(LEFT(yourColumnName, LOCATE('', yourColumnName)), '%m/%d/%Y') BETWEEN 'yourDateValue1' AND 'yourDateValue2’;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table SearchDateAsVarchar    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> ShippingDate varchar(100),    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.99 sec)Insert some records in the table using INSERT command. The query is as follows −mysql> insert into SearchDateAsVarchar(ShippingDate) values('6/28/2011 9:58 AM'); Query OK, 1 ... Read More

Can I use two where clauses like “SELECT * FROM table WHERE condition1 and condition2” in MySQL?

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

254 Views

Yes, you need to use AND or OR operator. The syntax is as follows −select *from yourTableName where yourColumnName1=yourValue AND yourColumnName=yourValue';For AND condition, both conditions must be true otherwise you will get an empty set.To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table WhereDemo -> ( -> Id int, -> Name varchar(20) -> ); Query OK, 0 rows affected (0.56 sec)Now you can insert some records in the table using insert command. The query is ... Read More

Resolve ERROR 1111 (HY000): Invalid use of group function in MySQL? How to correctly use aggregate function with where clause?

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

2K+ Views

To correctly use aggregate function with where clause in MySQL, the following is the syntax −select *from yourTableName where yourColumnName > (select AVG(yourColumnName) from yourTableName);To understand the above concept, let us create a table. The query to create a table is as follows −mysql> create table EmployeeInformation    -> (    -> EmployeeId int,    -> EmployeeName varchar(20),    -> EmployeeSalary int,    -> EmployeeDateOfBirth datetime    -> ); Query OK, 0 rows affected (1.08 sec)Now you can insert some records in the table using insert command. The query is as follows −mysql> insert into EmployeeInformation values(101, 'John', 5510, '1995-01-21'); ... Read More

How to select all rows from a table except the last one in MySQL?

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

2K+ Views

You need to use != operator along with subquery. The syntax is as follows −select *from yourTableName where yourIdColumnName != (select max(yourIdColumnName) from yourTableName );To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table AllRecordsExceptLastOne    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> UserName varchar(10),    -> UserAge int    -> ,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.65 sec)Now you can insert some records in the table using insert command. The query is as follows −mysql> ... Read More

Command to check read/write ratio in MySQL?

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

527 Views

To check the read/write ratio, you need to use SHOW STATUS command. This will give all the ratios.Case 1 − The syntax is as follows to get the read/write ratio −SHOW STATUS LIKE ‘Com_%’;Case 2 − If you want the insert, update, select and delete ratio, use the below syntax −SHOW GLOBAL STATUS WHERE Variable_name = 'Com_insert' OR Variable_name = 'Com_select' OR Variable_name = 'Com_Update' OR Variable_name = 'Com_delete';Here is no need to do any changes in the above syntax. The query is as follows −mysql> SHOW GLOBAL STATUS WHERE Variable_name = 'Com_insert' OR Variable_name = 'Com_select' OR    -> ... Read More

How to select part of a Timestamp in a MySQL Query?

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

130 Views

To select part of a timestamp in a query, you need to use YEAR() function. The syntax is as follows in MySQL.select YEAR(yourTimestampColumnName) as anyAliasName from yourTableName;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table SelectPartOfTimestampDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ShippingTime TIMESTAMP -> ); Query OK, 0 rows affected (1.11 sec)Now you can insert some records in the table using insert command. The query is as follows −mysql> ... Read More

Perform search/replace for only the first occurrence of a character with session variable in MySQL

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

143 Views

To perform search/ replace for only the first occurrence, use the CONCAT and REPLACE() function.The query is as follows to set user defined session variable −mysql> set @Sentence='Thks ks is a my string'; Query OK, 0 rows affected (0.00 sec)In this k will be replaced with i only once. The query is as follows. We have used INSTR() also −mysql> select @Sentence as NewString ,CONCAT(REPLACE(LEFT(@Sentence, INSTR(@Sentence, 'k')), 'k', 'i'),    -> SUBSTRING(@Sentence, INSTR(@Sentence, 'k') + 1)) as ChangeOnlyOneTime;The following is the output displaying only the first occurrence of a character is replaced −+------------------------+------------------------+ | NewString ... Read More

Advertisements