Found 4378 Articles for MySQL

Display dates after NOW() + 10 days from a MySQL table?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

592 Views

You can use DATE_ADD() function with where clause for this. Let us first create a table −mysql> create table DemoTable    (    ShippingDate date    ); Query OK, 0 rows affected (0.54 sec)Note : The current date and time is as follows, we found using NOW() −mysql> select now(); +-----------------------+ | now() | +-----------------------+ | 2019-06-04 20 :43 :57 | +-----------------------+ 1 row in set (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-06-16'); Query OK, 1 ... Read More

How to search between columns in MySQL?

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

109 Views

Use BETWEEN clause to search between columns. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Score1 int,    Score2 int    ); Query OK, 0 rows affected (0.78 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Score1, Score2) values(45, 65); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable(Score1, Score2) values(450, 680); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(Score1, Score2) values(800, 900); Query OK, 1 row affected (0.17 sec)Display all records from the table using select statement ... Read More

Concatenate two tables in MySQL with a condition?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

502 Views

To concatenate two tables, UNION ALL in MySQL. Let us create a table −mysql> create table DemoTable1    (    Id int,    FirstName varchar(20)    ); Query OK, 0 rows affected (1.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(10, 'John'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable1 values(20, 'Carol'); Query OK, 1 row affected (0.28 sec)Display all records from the table using select statement −mysql> select *from DemoTable1;Output+------+-----------+ | Id | FirstName | +------+-----------+ | 10 | John | ... Read More

How can I view the indexes I have set up in MySQL?

George John
Updated on 30-Jul-2019 22:30:26

76 Views

To view the indexes, you can use SHOW command.Following is the syntax −show index from yourTableName;Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(20),    LastName varchar(20)    ); Query OK, 0 rows affected (0.46 sec)Following is the query to create an index −mysql> create index id_FirstName on DemoTable(Id, FirstName); Query OK, 0 rows affected (0.52 sec) Records : 0 Duplicates : 0 Warnings : 0Following is the query to view the indexes I have set up in MySQL −mysql> show index from DemoTable;This will produce ... Read More

How to work multiple AND conditions in MySQL?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

1K+ Views

To work with multiple AND conditions in MySQL, following is the syntax −select *from yourTableName where yourColumnName1=yourValue1 and yourColumnName2=yourValue2 and yourColumnName3=yourValue3;Let us first create a table −mysql> create table DemoTable    (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(20),    StudentAge int,    StudentCountryName varchar(40)    ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentName, StudentAge, StudentCountryName) values('John', 23, 'US'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(StudentName, StudentAge, StudentCountryName) values('Carol', 21, 'UK'); Query OK, 1 row affected (0.15 sec) mysql> insert ... Read More

Check if table exists in MySQL and display the warning if it exists?

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

311 Views

To check if table exists, use the following syntax −CREATE TABLE IF NOT EXISTS yourTableName (    yourColumnName1 dataType,    .    .    .    .    N );Here, we will try to create a table that already exists and then it will produce a warning message “Table already exists”. Let us first create a table. This table already exists −mysql> CREATE TABLE IF NOT EXISTS DemoTable    (    Id int    ); Query OK, 0 rows affected, 1 warning (0.06 sec)The warning message is as follows −mysql> show warnings;Output+-------+------+-------------------------------------+ | Level | Code | Message ... Read More

How to apply NOW() to timestamps field in MySQL Workbench?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

727 Views

Let us first create a table −create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    ShippingDate timestamp );Insert some records in the table using insert command. Here, we have included the current date with NOW() −INSERT INTO DemoTable(ShippingDate) VALUES(now());Display all records from the table using select statement −SELECT *FROM DemoTable;OutputFollowing is the screenshot of query in MySQL workbench to set NOW() to timestamp field “ShippingDate”. The query also displays the output below −

MySQL automatic conversion on lowercase? Is this possible?

George John
Updated on 30-Jul-2019 22:30:26

289 Views

Yes, it is possible with triggers. You can create trigger for automatic conversion on lower case. Let us first create a table −mysql> create table DemoTable    (    StudentSubject text    ); Query OK, 0 rows affected (0.61 sec)Let us create a trigger for automatic conversion on lower case −mysql> CREATE TRIGGER lowerCaseOnInsertDemo BEFORE INSERT ON DemoTable FOR EACH ROW    SET NEW.StudentSubject = LOWER(NEW.StudentSubject); Query OK, 0 rows affected (0.21 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('MOngoDb'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('JaVA'); Query OK, ... Read More

How do I search for names starting with A in MySQL?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

110 Views

Use LIKE for this, as shown below −select *from yourTableName where yourColumnName LIKE 'A%';Let us first create a table −mysql> create table DemoTable    (    StudentName varchar(100)    ); Query OK, 0 rows affected (0.66 sec)Now you can insert some records in the table using insert command −mysql> insert into DemoTable values('John Smith'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('Adam Smith'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Aaron Taylor'); Query OK, 1 row affected (0.43 sec) mysql> insert into DemoTable values('Chris Brown'); Query OK, 1 row affected (0.27 sec)Display ... Read More

How to display two decimals in MySQL/PHP?

Arjun Thakur
Updated on 31-Dec-2019 08:52:20

276 Views

To display two decimals, use number_format(). Let us first write the PHP code for the same. We have first declared two variables are initialized −$number1=10.3423; $number2=10;Now, display the two decimals using the number_format() function −$result1=number_format ($number1, 2); $result2=number_format ($number2, 2);ExampleFollowing is the example −OutputFollowing is the snapshot of PHP code −10.34 10.00

Advertisements