我有Postgres查询,我可以使用laravel语法为“选择然后插入记录”创建查询。 无法在laravel查询中应用“如果存在,更新记录”。
postgre query
INSERT INTO product_details (id, name, hash_id )
SELECT prod.id, d.name, MD5(CONCAT(d.name, LOWER(prod.id)))
FROM tmpTable tmp
INNER JOIN detailsTable d USING(name)
INNER JOIN productsTable prod ON (prod.id = tmp.id)
ON CONFLICT ON CONSTRAINT product_details_pkey1
DO UPDATE SET name = EXCLUDED.name;
Laravel Query
$query = DB::table('tmpTable AS tmp')
->join('detailsTable AS d', 'tmp.name','=', 'd.name')
->join('productsTable AS prod', 'prod.id','=','tmp.id')
->select('prod.id', 'd.name','MD5(CONCAT(d.name, LOWER(prod.id)))');
DB::table('product_details')->insertUsing(['id', 'name', 'hash_id'], $query);