首页 >> 编程狮 >> 数据库

mysql入门(一)

1、主键。一列(或一组列),其值能够唯一区分表中每个行。唯一标识表

中每行的这个列(或这组列称为主键。主键用来表示一个特定的行。没有

主键,更新或删除表中特定行很困难。因为没有安全的方法保证只涉及相关的行。

2、sql语句不区分大小写,在处理sql语句时,其中所有空格都被忽略。

3、select distinct vend_id from products;只返回不同(唯一)的vend_id行

4、select prod_name from products limit 5; limit 5 指示mysql返回不多于5行

5、select prod_name from products limit 5,5; limit 5,5指示mysql返回从行5

开始的5行

6、使用完全限定表名

select products.prod_name  from products;

select products.prod_name from crashcourse.products;

7、下面的代码检索3个列,并按其中两个列对结果进行排序——首先按价格,

然后再按名称排序

select prod_id, prod_price, prod_name from products  order by 

prod_price,prod_name;

8、默认排序顺序是升序。为了进行降序,必须指定desc关键字。

select prod_id, prod_price,prod_name from products order by 

prod_price desc;

如果想在多个列上进行降序排序,必须对每个列指定desc关键字。

9、使用order by和limit的组合,能够找出一个列中最高或最低的值。

select prod_price from products order by prod_price desc limit 1;

10、数据根据where子句中指定的搜索条件进行过滤。

select prod_name, prod_price from products where prod_price=2.5;

where子句的位置:在同时使用order by和where子句时,应该让order by

位于where之后,否则会产生错误

select prod_name,prod_price from products where prod_price <10 ;

11、以下例子列出不是由供应商1003制造的所有产品

select vend_id,prod_name from products where vend_id <>1003;

12、范围值检查(检索价格在5美元和10美元之间的所有产品)

select prod_name,prod_price from products where prod_price between 5 and 10;

between匹配范围中所有的值,包括指定的开始值和结束值。




标签:

上一篇
mysql入门(二)

下一篇
没有了