Sharon Christine has Published 450 Articles

Comparing two columns in a single MySQL query to get one row?

Sharon Christine

Sharon Christine

Updated on 30-Jun-2020 13:42:26

For this, you can use ORDER BY clause. Let us first create a table −mysql> create table DemoTable -> ( -> Num1 int, -> Num2 int -> ); Query OK, 0 rows affected (0.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(60, 249); Query ... Read More

Searching for an integer value in a varchar field in MySQL?

Sharon Christine

Sharon Christine

Updated on 30-Jun-2020 13:37:17

To search for an integer value in a varchar filed, you can use CASE statement.Let us first create a table. Consider, we have a list of email-ids −mysql> create table DemoTable -> ( -> Title varchar(100) -> ); Query OK, 0 rows affected (0.61 sec)Insert some records in the table ... Read More

MySQL query to create table dynamically?

Sharon Christine

Sharon Christine

Updated on 30-Jun-2020 13:36:25

For this, you can use stored procedure. Let us create a table dynamically with two columns i.e. StudentId as int, whereas StudentName as varchar −mysql> DELIMITER $$ mysql> CREATE PROCEDURE creatingDynamicTableDemo(yourTableName VARCHAR(200))    -> BEGIN    ->    SET @name = yourTableName;    ->    SET @st = CONCAT('   ... Read More

MySQL query to fetch record by year

Sharon Christine

Sharon Christine

Updated on 30-Jun-2020 13:34:59

To fetch record by year, use the YEAR() method in MySQL −select *from yourTableName where year(yourColumnName)=yourYearValue;Let us first create a table −mysql> create table DemoTable -> ( -> CustomerName varchar(100), -> ShippingDate datetime -> ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command ... Read More

MySQL query with two boolean conditions to extract date based on hour?

Sharon Christine

Sharon Christine

Updated on 30-Jun-2020 13:24:50

Let us first create a table −mysql> create table DemoTable    -> (    -> AdmissionDate datetime    -> ); Query OK, 0 rows affected (0.75 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-10 10:45:10'); Query OK, 1 row affected (0.30 sec) mysql> ... Read More

Remove all except the first character of a string in MySQL?

Sharon Christine

Sharon Christine

Updated on 30-Jun-2020 13:24:00

For this, use the LEFT() method, which returns a specified number of characters from the left of the string.Let us first create a table −mysql> create table DemoTable    -> (    -> FirstName varchar(100)    -> ); Query OK, 0 rows affected (0.96 sec)Insert some records in the table ... Read More

How do I select 5 random rows from the 20 most recent rows in MySQL?

Sharon Christine

Sharon Christine

Updated on 30-Jun-2020 13:22:49

For random, use RAND() method. And for limit on rows, use the LIMIT() method.Let us first create a table −mysql> create table DemoTable    -> (    -> ShippingDate datetime    -> ); Query OK, 0 rows affected (0.51 sec)Insert some records in the table using insert command −mysql> insert ... Read More

How to get everything before the last occurrence of a character in MySQL?

Sharon Christine

Sharon Christine

Updated on 30-Jun-2020 12:55:53

You can use below syntax. Following is the syntax −update yourTableName set yourColumnName=REVERSE(SUBSTRING(REVERSE(yourColumnName), INSTR(REVERSE(yourColumnName), '.')));Let us first create a table −mysql> create table DemoTable -> ( -> Words text -> ); Query OK, 0 rows affected (0.51 sec)Insert some records in the table using insert command −mysql> insert into DemoTable ... Read More

How to update data in a MySQL database without removing the old data?

Sharon Christine

Sharon Christine

Updated on 30-Jun-2020 12:39:56

For this, you can use UPDATE and concatenate the new data with the old one to save the old data as well −update yourTableName set yourColumnName=concat(yourColumnName, ", yourValue");Let us first create a table −mysql> create table DemoTable -> ( -> CustomerName varchar(100) -> ); Query OK, 0 rows affected (0.54 ... Read More

MySQL query to return the entire date and time based on a string and format

Sharon Christine

Sharon Christine

Updated on 30-Jun-2020 12:36:03

Let us first create a table −mysql> create table DemoTable    -> (    -> AdmissionDate varchar(100)    -> ); Query OK, 0 rows affected (0.66 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Wed, 19 Jun 2019 04:10:20'); Query OK, 1 row affected (0.22 ... Read More

Previous 1 ... 5 6 7 8 9 ... 45 Next
Advertisements