1.一些重要的SQL命令:
SELECT - 从数据库中提取数据
UPDATE - 更新数据库中的数据
DELETE - 从数据库中删除数据
INSERT INTO - 向数据库中插入新数据
CREATE DATABASE - 创建新数据库
ALTER DATABASE - 修改数据库
CREATE TABLE - 创建新表
ALTER TABLE - 变更(改变)数据库表
DROP TABLE - 删除表
CREATE INDEX - 创建索引(搜索键)
DROP INDEX - 删除索引
2. SELECT DISTINCT 语句用于返回唯一不同的值
Select DISTINCT piaoshifu_goods.id 商品编号,piaoshifu_goods.match 时间 from piaoshifu_goods where piaoshifu_goods.seat_total = 50
3.SQL中可以使用and和or关键字帮助查询。
例子:
select DISTINCT pcr.type 类型,pcr.name 姓名 from piaoshifu_cms_role as pcr where pcr.name <> '经理' and pcr.update_datetime='2015-02-05 15:50:45' or pcr.update_datetime='2015-02-06 12:10:45'
4. SQL的ORDER BY关键字:
例子1:
SELECT po.user_id 用户编号,po.id 订单编号 ,po.type 订单类型 ,po.payment_datetime 支付时间, po.total_price 支付金额 FROM piaoshifu_order as po where po.user_id = 1 order by po.total_price DESC #按照支付金额的从大到小排序accessible,按照一个 字段排序
例子2:
SELECT po.user_id 用户编号,po.id 订单编号 ,po.type 订单类型 ,po.payment_datetime 支付时间, po.total_price 支付金额 FROM piaoshifu_order as po where po.user_id = 1 order by po.total_price , po.payment_datetime #按照支付金额的从大到小排序,按照两个字段排序
ORDER BY 关键字用于对结果集按照一个列或者多个列进行排序。
ORDER BY 关键字默认按照升序对记录进行排序。如果需要按照降序对记录进行排序,您可以使用 DESC 关键字。
5. INSERT INTO用于向表中插入数据:
INSERT INTO 语句可以有两种编写形式。
第一种形式无需指定要插入数据的列名,只需提供被插入的值即可:
VALUES (value1,value2,value3,...);
Insert into piaoshifu_cms_role values(11,4,'dashen',123456,'wyl','0000-00-00 00:00:00','0000-00-00 00:00:00',0);
第二种形式需要指定列名及被插入的值:
VALUES (value1,value2,value3,...);
insert into piaoshifu_cms_role(id,type,name,password,handler,create_datetime,update_datetime,delete_flg) values(12,5,'dashan',123456,'wyyl','0000-00-00 00:00:00','0000-00-00 00:00:00',0);
6.UPDATE 语句用于更新表中的记录:
UPDATE 语句用于更新表中已存在的记录。
SQL UPDATE 语法
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
例子:
update piaoshifu_cms_role set id = 13,type = 5 where id = 12
7.DELETE 语句用于删除表中的记录。
DELETE 语句用于删除表中的行。
SQL DELETE 语法
WHERE some_column=some_value;
DELETE from piaoshifu_cms_role where id = 11;
您可以在不删除表的情况下,删除表中所有的行。这意味着表结构、属性、索引将保持不变:
or
DELETE * FROM table_name;
8.SELECT TOP 子句用于规定要返回的记录的数目
SELECT TOP 子句对于拥有数千条记录的大型表来说,是非常有用的。
#mysql数据库 SELECT * FROM piaoshifu_order limit 10
9. LIKE 操作符用于在 WHERE 子句中搜索列中的指定模式。
FROM table_name
WHERE column_name LIKE pattern;
select * from piaoshifu_order where payment_datetime like '2015%' and delivery_name like 'T%'
例子2:
select * from piaoshifu_order where payment_datetime like '2015%' and delivery_name not like 'T%'
10. N 操作符允许您在 WHERE 子句中规定多个值。
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...);
SELECT * from piaoshifu_order where user_id in(1,3) SELECT * from piaoshifu_order where total_price in (100,1000,200,300)
11. BETWEEN 操作符选取介于两个值之间的数据范围内的值。这些值可以是数值、文本或者日期。
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
SELECT * FROM piaoshifu_order Where total_price between '100' and '1000' SELECT piaoshifu_order.real_total_price FROM piaoshifu_order where piaoshifu_order.real_total_price between '10' and '100'
例子2:
select * from piaoshifu_order where (total_price between 100 and 1000) and not status in(41,81)
例子3:not between
SELECT piaoshifu_order.real_total_price FROM piaoshifu_order where piaoshifu_order.real_total_price not between '10' and '100'
12.SQL的链接:
SQL JOIN 子句用于把来自两个或多个表的行结合起来,基于这些表之间的共同字段。
最常见的 JOIN 类型:SQL INNER JOIN(简单的 JOIN)。 SQL INNER JOIN 从多个表中返回满足 JOIN 条件的所有行
select po.id 订单编号,pcc.id 点卡编号,po.type 订单类型 from piaoshifu_order as po inner join piaoshifu_credit_card_consume_history pcc on po.id = pcc.id
select po.id 订单编号,pccch.user_id 用户编号 from piaoshifu_order as po inner join piaoshifu_credit_card_consume_history as pccch on pccch.id = po.id order by pccch.user_id
13. LEFT JOIN 关键字从左表(table1)返回所有的行,即使右表(table2)中没有匹配。如果右表中没有匹配,则结果为 NULL。