How to convert a string to date in MySQL?

As was told at MySQL Using a string column with date text as a date field, you can do You can also handle these date strings in WHERE clauses. For example You can handle all kinds of date/time layouts this way. Please refer to the format specifiers for the DATE_FORMAT() function to see what you … Read more

How to SUM and SUBTRACT using SQL?

I think this is what you’re looking for. NEW_BAL is the sum of QTYs subtracted from the balance: If you want to update the item balance with the new balance, use the following: This assumes you posted the subtraction backward; it subtracts the quantities in the order from the balance, which makes the most sense … Read more

MySQL AS keyword

I’m not professional in query writing, but wrote many from the time I began MySQL. Recently I noticed there is no need to type AS keyword in name aliasing. equals to However I know that this ability is out from years ago. I’ve 2 questions around this subject: Is AS keyword redundant? Sometimes ago when … Read more

Remove duplicate rows in MySQL

A really easy way to do this is to add a UNIQUE index on the 3 columns. When you write the ALTER statement, include the IGNORE keyword. Like so: This will drop all the duplicate rows. As an added benefit, future INSERTs that are duplicates will error out. As always, you may want to take a backup before running something like this…

LAST_INSERT_ID() MySQL

You could store the last insert id in a variable : Or get the max id from table1 (EDIT: Warning. See note in comments from Rob Starling about possible errors from race conditions when using the max id) (Warning: as Rob Starling points out in the