AmitDiwan has Published 10740 Articles

Get only a single value from a specific MySQL row?

AmitDiwan

AmitDiwan

Updated on 27-Dec-2019 07:02:32

2K+ Views

For this, use SELECT INTO variable with where clause. Let us first create a table −mysql> create table DemoTable1896    (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(20),    StudentMarks int    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using ... Read More

What to assign to a MySQL column that must not be empty?

AmitDiwan

AmitDiwan

Updated on 27-Dec-2019 06:58:48

430 Views

Define with NOT NULL, if a column must not be empty. Let us first create a table with one of the columns as NOT NULL −mysql> create table DemoTable1895    (    Id int NOT NULL,    FirstName varchar(20),    LastName varchar(20) NOT NULL    ); Query OK, 0 rows ... Read More

Set a MySQL field with the current date (UNIX_TIMESTAMP(now))

AmitDiwan

AmitDiwan

Updated on 27-Dec-2019 06:57:19

595 Views

For this, use unix_timestamp(). Let us first create a table −mysql> create table DemoTable1894    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    DueTime int    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1894 values(); Query ... Read More

Can you allow a regex match in a MySQL Select statement?

AmitDiwan

AmitDiwan

Updated on 27-Dec-2019 06:51:28

193 Views

Yes, we can do regex match in a select statement −select yourColumnName from yourTableName where yourColumnName regexp '^yourValue';Let us first create a table −mysql> create table DemoTable1892    (    FirstName varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> ... Read More

MySQL Stored procedure to declare two values and perform mathematical operation

AmitDiwan

AmitDiwan

Updated on 27-Dec-2019 06:50:21

943 Views

Let us first create a stored procedure −mysql> delimiter // mysql> create procedure declare_demo_sp()    begin    declare Value1 int;    declare Value2 int;    set Value1=100;    set Value2=2000;    select Value1, Value2, Value1*Value2 as MultiplicationResult;    end    // Query OK, 0 rows affected (0.00 sec) mysql> delimiter ... Read More

Check if MySQL entry exists and if it does, how to overwrite other columns?

AmitDiwan

AmitDiwan

Updated on 27-Dec-2019 06:48:48

263 Views

For this, use INSERT ON DUPLICATE KEY UPDATE command. Let us first create a table −mysql> create table DemoTable1891    (    FirstName varchar(20),    UNIQUE KEY(FirstName)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1891 values('Chris') on ... Read More

Select data and set value to boolean based on timestamp column in MySQL

AmitDiwan

AmitDiwan

Updated on 27-Dec-2019 06:47:30

420 Views

For this, use IF(). Let us first see the current date −mysql> select curdate(); +------------+ | curdate()  | +------------+ | 2019-12-10 | +------------+ 1 row in set (0.00 sec)Let us first create a table −mysql> create table DemoTable1890    (    DueDate timestamp    ); Query OK, 0 rows affected ... Read More

How to sum current month records in MySQL?

AmitDiwan

AmitDiwan

Updated on 27-Dec-2019 06:46:05

995 Views

To sum current month records, use the SUM() and MONTH() function. Let us first create a table −mysql> create table DemoTable1889    (    DueDate date,    Amount int    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1889 ... Read More

MySQL query to update all entries with md5 version of name?

AmitDiwan

AmitDiwan

Updated on 27-Dec-2019 06:43:27

902 Views

For this, you can use MD5(). Let us first create a table −mysql> create table DemoTable1887    (    Password text,    HashPassword text    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1887(Password) values('John@9089'); Query OK, 1 row ... Read More

How do you get whether a column is a primary key in MySQL?

AmitDiwan

AmitDiwan

Updated on 27-Dec-2019 06:41:32

492 Views

To get whether a column is a primary key, use COLUMN_NAME and COLUMN_KEY='PRI'. With that, the entire syntax is as follows −select column_name, case when column_key= 'PRI' then 'yourMessage1' else ''yourMessage2' end as anyAliasName from information_schema.columns where table_schema =database() and `table_name` = yourTableName order by `table_name`, ordinal_position;To understand the above ... Read More

Advertisements