Showing posts with label sql. Show all posts
Showing posts with label sql. Show all posts

Saturday, December 31, 2016

Laravel insert ignore check if inserted (affected rows for insert ignore)

Halo guys!
if you break your head for finding solution to check if row is inserted here is the way

$query = "INSERT INTO table (id) VALUES (?)";
    try {
        DB::insert($query, array($value));
        return 1;
    } catch (\Exception $e) {
        return 0;
    }

The solution is simle - not use insert ignore. So we are catching exception instead of fatal erroring and know if it did or not.
You can even do query builder insert instead of raw query.

Sunday, November 13, 2016

SQL select where join not existed; Select where can't join

SELECT t1.ID
FROM Table1 t1
    LEFT JOIN Table2 t2 ON t1.ID = t2.ID
WHERE t2.ID IS NULL
source: http://stackoverflow.com/a/4076157/1767461

Sunday, October 2, 2016

Friday, September 9, 2016

The proper way of doing SQL "INSERT ON DUPLICATE UPDATE"

$sql = 'INSERT INTO table_name (column1, column2, column3, column4) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE column1=VALUES(column1), column2=VALUES(column2)';

Tuesday, August 23, 2016