basic sql operation


碰到有过山炮电话里直接问啥啥操作SQL怎么写的,这几年没写过,连syntax都忘记了。记下来备用。

insert 的两种形式

1
2
INSERT INTO table_name
VALUES (value1,value2,value3,...);

1
2
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);

SQL UPDATE Syntax

1
2
3
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;

SQL IN Syntax

1
2
3
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...);

Different SQL JOINs

1
2
3
4
INNER JOIN: Returns all rows when there is at least one match in BOTH tables
LEFT JOIN: Return all rows from the left table, and the matched rows from the right table
RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table
FULL JOIN: Return all rows when there is a match in ONE of the tables

The UNION operator is used to combine the result-set of two or more SELECT statements.

Notice that each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order.

SQL UNION Syntax

1
2
3
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;