Found 6702 Articles for Database

Find date record after a particular date from a column with VARCHAR type in MySQL

AmitDiwan
Updated on 08-Nov-2019 10:55:46

126 Views

Let us first create a table. Here, we have set date as VARCHAR −mysql> create table DemoTable1364     -> (     -> ShippingDate varchar(100)     -> ); Query OK, 0 rows affected (0.87 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1364 values('01-09-2019 12:34:55'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1364 values('01-07-2018 17:10:00'); Query OK, 1 row affected (0.37 sec) mysql> insert into DemoTable1364 values('24-09-2019 10:31:22'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable1364 values('20-09-2018 13:00:00'); Query OK, 1 row affected (0.13 sec)Display all records from ... Read More

Using MySQL IN() for some column values with underscore

AmitDiwan
Updated on 08-Nov-2019 10:53:26

128 Views

Let us first create a table −mysql> create table DemoTable1363     -> (     -> StudentId varchar(20)     -> ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1363 values('901'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1363 values('702'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1363 values('901_John_Doe'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1363 values('1001_Carol_Taylor'); Query OK, 1 row affected (0.26 sec)Display all records from the table using select statement −mysql> select * from DemoTable1363;This will ... Read More

MySQL permissions to view all databases?

AmitDiwan
Updated on 08-Nov-2019 10:51:40

197 Views

For this, the syntax is as follows −revoke show databases on *.* from 'yourUserName'@'yourHostName';Let us display all usernames along with host name −mysql> select user, host from MySQL.user;This will produce the following output −+------------------+-----------+ | user             | host      | +------------------+-----------+ | Bob              | %         | | Charlie          | %         | | Robert           | %         | | User2            | %   ... Read More

Create variables in MySQL stored procedure with DECLARE keyword

AmitDiwan
Updated on 08-Nov-2019 10:49:23

565 Views

Use MySQL DECLARE for variables in stored procedure −DECLARE anyVariableName int DEFAULT anyValue;Let us implement the above syntax in order to create variables in stored procedure −mysql> DELIMITER // mysql> CREATE PROCEDURE variable_Demo()     -> BEGIN     -> DECLARE lastInsertedId int DEFAULT -1;     -> select lastInsertedId;     -> set @providedLastId=10001;     -> select @providedLastId;     -> END     -> // Query OK, 0 rows affected (0.32 sec) mysql> DELIMITER ;Now you can call the above stored procedure using CALL command −mysql> call variable_Demo();This will produce the following output −+----------------+ | lastInsertedId | ... Read More

Treat a MySQL column field as NULL if it is blank?

AmitDiwan
Updated on 08-Nov-2019 10:47:17

151 Views

Let us first create a table −mysql> create table DemoTable1362     -> (     -> ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,     -> ClientName varchar(40)     -> ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1362(ClientName) values('Chris'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1362(ClientName) values(' '); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable1362(ClientName) values('Bob'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1362(ClientName) values(' '); Query OK, 1 row affected (0.12 sec) mysql> insert ... Read More

How to find all tables that contains two specific columns in MySQL?

AmitDiwan
Updated on 08-Nov-2019 10:45:23

590 Views

To find two specific column names, use information_schema.columns Here, I am using Id in place of columnA and Name in place of columnB −mysql> select table_name as TableNameFromWebDatabase    -> from information_schema.columns    -> where column_name IN ('Id', 'Name')    -> group by table_name    -> having count(*) = 3;This will produce the following output. Following are the tables with columns Id and Name −+--------------------------+ | TableNameFromWebDatabase | +--------------------------+ | student                  | | distinctdemo             | | secondtable              | | groupconcatenatedemo ... Read More

Delete URLs with specific domains from MySQL database?

AmitDiwan
Updated on 08-Nov-2019 10:39:25

227 Views

To delete URLs with specific domains, use DELETE and LIKE clause.Let us first create a table −mysql> create table DemoTable1361     -> (     -> URL text     -> ) ; Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1361 values('Https://www.google.com//?id=1'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1361 values('Https://www.facebook.com//?id=2&name=John'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1361 values('Https://www.yahoo.com//?id=3'); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable1361 values('Https://www.google.com//?id=1'); Query OK, 1 row affected (0.16 sec)Display all records from ... Read More

Get current year in MySQL WHERE clause?

AmitDiwan
Updated on 08-Nov-2019 10:37:28

611 Views

To get current year, use YEAR() along with CURDATE(). Let us first create a table −mysql> create table DemoTable1360     -> (     -> JoiningYear int     -> )     -> ; Query OK, 0 rows affected (0.51 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1360 values(1998); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1360 values(2018); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable1360 values(2016); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1360 values(2019); Query OK, 1 row affected (0.13 sec) mysql> ... Read More

Using backticks in CONTACT() gives an error in MySQL

AmitDiwan
Updated on 08-Nov-2019 10:35:10

88 Views

Do not use backticks, you can use single quotes in CONCAT(). Following is the syntax −select concat(yourColumnName1, ' ', yourColumnName2) from yourTableName;Let us first create a table −mysql> create table DemoTable1359     -> (     -> Id int,     -> Name varchar(20)     -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1359 values(101, 'Chris'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1359 values(102, 'Adam'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1359 values(103, 'Mike'); Query OK, 1 row ... Read More

Sum values in MySQL from similar day records

AmitDiwan
Updated on 05-Nov-2019 10:19:27

694 Views

Use GROUP BY and DATE() for this. Let us first create a table −mysql> create table DemoTable1358     -> (     -> PurchaseDate datetime,     -> ProductPrice int     -> ); Query OK, 0 rows affected (1.59 sec)Insert some records in the table using insert command. Here, we have inserted date records, with some records with similar dates −mysql> insert into DemoTable1358 values('2019-09-20 12:34:00', 450); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable1358 values('2019-09-21 11:00:00', 1050); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable1358 values('2018-09-21 02:10:00', 2050); Query OK, 1 ... Read More

Advertisements