文中的大部分SQL摘自MySQL Tutorial
INSERT INTO table_name(column_list)
SELECT
select_list
FROM
another_table
WHERE
condition;
如果目標表裡已經存在相同的主鍵,則執行下面 下麪的更新欄位的SQL
INSERT INTO table (column_list)
VALUES (value_list)
[SELECT ...FROM ... WHERE]
ON DUPLICATE KEY UPDATE
c1 = v1,
c2 = v2,
...;
UPDATE T1, T2,
[INNER JOIN | LEFT JOIN] T1 ON T1.C1 = T2. C1
SET T1.C2 = T2.C2,
T2.C3 = expr
WHERE condition
DELETE T1[, T2]
FROM T1
INNER JOIN T2 ON T1.key = T2.key
WHERE condition;
SELECT pk,c1 FROM (
SELECT t1.pk, t1.c1
FROM t1
UNION ALL
SELECT t2.pk, t2.c1
FROM t2
) t
GROUP BY pk, c1
HAVING COUNT(0) = 1
ORDER BY pk
SELECT
col,
COUNT(col)
FROM
table_name
GROUP BY col
HAVING COUNT(col) > 1;
@Variable_name
呼叫)SET @variable_name := value;
SELECT @variable_name := value;
SELECT
productname
FROM
products
WHERE
productname REGEXP '^(A|B|C)'
ORDER BY productname;
Metacharacter | Behavior |
---|---|
^ | matches the position at the beginning of the searched string |
$ | matches the position at the end of the searched string |
. | matches any single character |
[…] | matches any character specified inside the square brackets |
[^…] | matches any character not specified inside the square brackets |
p1|p2 | matches any of the patterns p1 or p2 |
* | matches the preceding character zero or more times |
+ | matches preceding character one or more times |
{n} | matches n number of instances of the preceding character |
{m,n} | matches from m to n number of instances of the preceding character |
查詢數據的時候 REGEXP,LIKE,IN,=都可以查詢
但是需要考慮:效率哪個更高
ORDER BY
FIELD(key,'value','value2','value3');
需要特殊注意:如果排序欄位的值不在上述列出,則會排在最前面