Found 4378 Articles for MySQL

Display first selected row in MySQL?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

142 Views

You can use ORDER BY for this. Let us first create a table −mysql> create table DemoTable    (    Value int    ); Query OK, 0 rows affected (0.20 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable values(8); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(18); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable values(11); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(9); Query OK, 1 row ... Read More

Select and add result of multiplying two columns from a table in MySQL?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

590 Views

You can use aggregate function SUM() for this. Let us first create a table −mysql> create table DemoTable    (    CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    CustomerProductName varchar(100),    CustomerProductQuantity int,    CustomerPrice int    ); Query OK, 0 rows affected (0.17 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(CustomerProductName, CustomerProductQuantity, CustomerPrice) values('Product-1', 5, 400); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(CustomerProductName, CustomerProductQuantity, CustomerPrice) values('Product-2', 3, 100); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable(CustomerProductName, CustomerProductQuantity, CustomerPrice) values('Product-1', 2, 300); Query OK, 1 ... Read More

MySQL query to search exact word from string?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

2K+ Views

To search exact word from string, use the below syntax −select *from yourTableName where yourColumnName regexp '(^|[[:space:]])yourWord([[:space:]]|$)';Let us first create a table −mysql> create table DemoTable    (    Title text    ); Query OK, 0 rows affected (0.23 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('This is the Introduction to Java'); Query OK, 1 row affected (0.05 sec) mysql> insert into DemoTable values('This is the Introduction to MongoDB'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable values('This is the Introduction to MySQL'); Query OK, 1 row affected (0.06 ... Read More

Does MySQL DROP TABLE completely remove the table or just the structure?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

76 Views

The DROP TABLE removes the table completely and also removes all data. If you want to remove all data completely and wants the table structure, then you can use TRUNCATE TABLE command. The TRUNCATE command will recreate the table.Let us first check the DROP TABLE. For that, we will first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(20)    ); Query OK, 0 rows affected (0.20 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name) values('Larry'); Query OK, 1 row affected (0.07 sec) ... Read More

Select random number from a specific list in MySQL?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

1K+ Views

You can use elt() along with rand() for this. Let us select random number from a specific list.mysql> SELECT ELT(FLOOR(RAND() * 10) + 1,    100, 200, 300, 400, 500, 600, 700, 800, 900, 1000) AS random_value_from_listOfValues;This will produce the following output −+--------------------------------+ | random_value_from_listOfValues | +--------------------------------+ | 1000 | +--------------------------------+ 1 row in set (0.00 sec)Now we will run the query again to select random number from a specific list.mysql> SELECT ELT(FLOOR(RAND() * ... Read More

How to store time created in a MySQL table?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

137 Views

You can use DEFAULT CURRENT_TIMESTAMP. Keep in mind that it will work only at the time of insertion. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Arrivaltime TIMESTAMP DEFAULT CURRENT_TIMESTAMP    ); Query OK, 0 rows affected (0.31 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Arrivaltime) values('2018-01-31 10:34:56'); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable(Arrivaltime) values('2019-01-31 11:10:12'); Query OK, 1 row affected (0.04 sec)Display ... Read More

What is the MySQL alias shorthand?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

198 Views

You need to give name explicitly or you can remove AS command. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(20)    ); Query OK, 0 rows affected (0.21 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name) values('John'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(Name) values('Larry'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(Name) values('Sam'); Query OK, 1 row affected (0.06 sec)Display all records from the table using select statement −mysql> select ... Read More

How to fetch only N rows at a time in MySQL?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

559 Views

To fetch only N rows at a time, you can use LIMIT operator. Following is the syntax −select *from yourTableNameLIMIT 0, N;Let us first create a table −mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(20) ); Query OK, 0 rows affected (0.25 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name) values('John'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable(Name) values('Larry'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(Name) values('Bob'); ... Read More

Get the sum of a column in all MySQL rows?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

168 Views

Use aggregate function SUM() to get the sum of a column in al rows. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Amount int    ); Query OK, 0 rows affected (0.20 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Amount) values(50); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(Amount) values(60); Query OK, 1 row affected (0.04 sec) mysql> insert into DemoTable(Amount) values(70); Query OK, 1 row affected (0.10 sec)Display all records from the table using select statement −mysql> ... Read More

How to insert only those records that does not exist in a MySQL table?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

113 Views

To insert when a record does not exist, set the column as UNIQUE INDEX. 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.30 sec)Following is the query to create unique index to insert record which does not exist in the ‘FirstName’ column −mysql> CREATE UNIQUE INDEX index_on_FirstName ON DemoTable(FirstName); Query OK, 0 rows affected (0.56 sec) Records: 0 Duplicates: 0 Warnings: 0Insert some records in the table using insert command. ... Read More

Advertisements