Found 4219 Articles for MySQLi

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

112 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

How do I split a numerical query result in MySQL?

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

103 Views

To split a numerical query result, you can use the CONCAT() function in MySQL. Let us first create a table −mysql> create table DemoTable    (    StudentId int    ); Query OK, 0 rows affected (0.68 sec)Now you can insert some records in the table using insert command −mysql> insert into DemoTable values(2222); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(5555); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(4567); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(8905); Query OK, 1 row affected (0.15 sec)Display all records from ... Read More

Is name a reserved word in MySQL?

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

245 Views

No, name is not a reserved word in MySQL, you can use without backtick symbol. If you are working on a reserved word then use backtick symbol. Let us first create a table −mysql> create table name    (    name varchar(10)    ); Query OK, 0 rows affected (0.78 sec)Now you can insert some records in the table using insert command −mysql> insert into name values('John'); Query OK, 1 row affected (0.13 sec) mysql> insert into name values('Carol'); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> select *from name;Output+-------+ | name ... Read More

How to sort by value with MySQL ORDER BY?

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

149 Views

For this, use the ORDER BY clause. Let us first create a table −mysql> create table DemoTable    (    StudentId int    ); Query OK, 0 rows affected (0.59 sec)Now you can insert some records in the table using insert command −mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(60); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(70); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(45); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values(55); Query OK, 1 row affected ... Read More

How to insert mm/dd/yyyy format dates in MySQL?

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

5K+ Views

For this, use STR_TO_DATE(). Following is the syntax −insert into yourTableName values(STR_TO_DATE(yourDateValue, yourFormatSpecifier));Let us first create a table −mysql> create table DemoTable    (    ShippingDate date    ); Query OK, 0 rows affected (0.81 sec)Insert some records in the table using insert command : Here, we are inserting formatted dates using date formats like m, d, y, etc −mysql> insert into DemoTable values(STR_TO_DATE('06-01-2019', '%m-%d-%Y')); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(STR_TO_DATE('01-31-2019', '%m-%d-%Y')); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(STR_TO_DATE('02-01-2018', '%m-%d-%Y')); Query OK, 1 row affected (0.27 sec)Display all records ... Read More

How to get day name from timestamp in MySQL?

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

233 Views

To get the day name from timestamp, use dayname() function −select dayname(yourColumnName) from yourTableName;    Let us first create a table :    mysql> create table DemoTable    (    LoginDate timestamp    ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-06-01'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('2019-06-02'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('2019-06-03'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('2019-06-04'); Query OK, 1 row affected (0.11 sec) mysql> insert into ... Read More

Advertisements