Chandu yadav has Published 1163 Articles

MySQL date column auto fill with current date?

Chandu yadav

Chandu yadav

Updated on 30-Jul-2019 22:30:24

13K+ Views

You can use now() with default auto fill and current date and time for this. Later, you can extract the date part using date() function.Case 1:The syntax is as follows:yourDateColumnName date default ‘yourDateValue’;Case 2:The syntax is as follows:yourDateColumnName datetime default now();To understand the above, let us create a table. The ... Read More

MySQL Select to get users who have logged in today?

Chandu yadav

Chandu yadav

Updated on 30-Jul-2019 22:30:24

305 Views

To get the users logged in today, use the below syntax. Here, we are expecting that your datetime field is a string type −select yourColumnName1, yourColumnName2, yourColumnName3, ...N from youTableName WHERE STR_TO_DATE(yourColumnName1, ‘format’') =CURDATE();Let’s say we have the following “DateEqualToday “ table that stores users first and last name with ... Read More

Remove all the elements from an ArrayList that are in another Collection in Java

Chandu yadav

Chandu yadav

Updated on 30-Jul-2019 22:30:24

108 Views

The method java.util.ArrayList.removeAll() removes all the the elements from the ArrayList that are available in another collection. This method has a single parameter i.e. the Collection whose elements are to be removed from the ArrayList.A program that demonstrates this is given as followsExample Live Demoimport java.util.ArrayList; import java.util.List; public class Demo ... Read More

Using single quotes around database and table name isn’t working in MySQL?

Chandu yadav

Chandu yadav

Updated on 30-Jul-2019 22:30:24

514 Views

You need to use backticks around table name as well as database name. The syntax is as follows:UPDATE `yourDatabaseName`.`yourTableName` SET yourColumnName1=yourColumnName1+1 WHERE yourColumnName2=’yourValue’;To understand the above syntax, let us create a table. The query to create a table is as follows:mysql> use test; Database changed mysql> create table Add1Demo   ... Read More

How to Reset MySQL AutoIncrement using a MAX value from another table?

Chandu yadav

Chandu yadav

Updated on 30-Jul-2019 22:30:24

971 Views

You can use prepare statement to Reset MySQL AutoIncrement using a MAX value from another table.The following is the syntax −set @anyVariableName1=(select MAX(yourColumnName) from yourTableName1); SET @anyVariableName2 = CONCAT('ALTER TABLE yourTableName2 AUTO_INCREMENT=', @anyVariableName1); PREPARE yourStatementName FROM @anyVariableName2; execute yourStatementName;The above syntax will reset MySQL auto_increment using a maximum value from ... Read More

Can MySQL automatically store timestamp in a row?

Chandu yadav

Chandu yadav

Updated on 30-Jul-2019 22:30:24

2K+ Views

Yes, you can achieve this in the following two ways.First Approach At the time of creation of a table.Second Approach At the time of writing query.The syntax is as follows.CREATE TABLE yourTableName ( yourDateTimeColumnName datetime default current_timestamp );You can use alter command.The syntax is as follows.ALTER TABLE yourTableName ADD yourColumnName ... Read More

How to delete last record (on condition) from a table in MySQL?

Chandu yadav

Chandu yadav

Updated on 30-Jul-2019 22:30:24

7K+ Views

To delete last record (on condition) from a table, you need to use ORDER BY DESC with LIMIT 1. The syntax is as follows:DELETE FROM yourTableName WHERE yourColumnName1=yourValue ORDER BY yourColumnName2 DESC LIMIT 1;The above syntax will delete last record (on condition) from a table. It sorts the column in descending ... Read More

MySQL increment a database field by 1?

Chandu yadav

Chandu yadav

Updated on 30-Jul-2019 22:30:24

4K+ Views

You can increment a database using update command. The syntax is as follows −UPDATE yourTableName set yourColumnName=yourColumnName+1 where condition;To understand the above syntax, let us first create a table. The query to create a table is as follows −mysql> create table IncrementBy1    -> (    -> Id int,   ... Read More

Change MySQL default character set to UTF-8 in my.cnf?

Chandu yadav

Chandu yadav

Updated on 30-Jul-2019 22:30:24

1K+ Views

To change MySQL default character set to UTF-8 in my.cnf, firstly reach the location of my.cnf file.The following is the screenshot of “my.cnf” file. Firstly, open the C: directory and the “Program Data” folder −Now, click on “MySQL” folder −Now, click the MySQL Server 8.0 folder and open it −After ... Read More

Get Last Entry in a MySQL table?

Chandu yadav

Chandu yadav

Updated on 30-Jul-2019 22:30:24

1K+ Views

You can get the last entry in a MySQL table using ORDER BY. The first approach is as follows:Case 1: Using DESC LIMITSELECT * FROM yourTableName ORDER BY yourColumnName DESC LIMIT 1;The second approach is as follows:Case 2: Using MAX()SET @anyVariableName = (SELECT MAX(yourColumnName) FROM yourTableName); SELECT *FROM yourtableName WHERE ... Read More

Advertisements