[SQL] MySQL to PostgreSQL 好痛轉移,SQL語法差異

換資料庫真的好機掰,會有一堆未知問題,見招拆招,紀錄於下

LIMIT OFFSET

/* MySQL */
SELECT * FROM abc WHERE status=1 ORDER BY create_time DESC LIMIT , 10
/* PostgreSQL */
SELECT * FROM abc WHERE status=1 ORDER BY create_time DESC LIMIT 10 OFFSET

# UPDATE 不能加 ORDER BY and LIMIT
/* MySQL */
UPDATE abc SET aaa=1 WHERE id<=100 ORDER BY status DESC LIMIT 10

/* PostgreSQL */
/* 若有需求要用子句解決 */
UPDATE abc main SET aaa=1
FROM
 ( SELECT id FROM page_builder WHERE id<=100 ORDER BY status DESC LIMIT 10 ) sub
WHERE main.id = sub.id

PHP

//mysql 取新增的id
mysql_insert_id() mysqli_insert_id();
//PostgreSQL

$re = pg_query($conn, "INSERT INTO foo (bar) values (123);");
$re
= pg_query("SELECT lastval();");
$row
= pg_fetch_row($re);
$insert_id
= $row
[];

相關文章:

READ  新增or修改網址url參數

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料