当前位置:Gxlcms > 数据库问题 > MySQL数据库基本查询(一)

MySQL数据库基本查询(一)

时间:2021-07-01 10:21:17 帮助过:22人阅读

1、创建所需表及插入数据

  1. <code>#创建数据表
  2. mysql> create table fruits ( f_id char(10) not null, s_id int not null, f_name char(255) not null, f_price decimal(8,2) not null, primary key(f_id) );
  3. #插入数据
  4. mysql> insert into fruits(f_id,s_id,f_name,f_price)
  5. -> values(‘a1‘,101,‘apple‘,‘5.2‘),
  6. -> (‘b1‘,101,‘blackberry‘,‘10.2‘),
  7. -> (‘bs1‘,102,‘orange‘,‘11.2‘),
  8. -> (‘bs2‘,105,‘melon‘,‘8.2‘),
  9. -> (‘t1‘,102,‘banana‘,‘10.3‘),
  10. -> (‘t2‘,102,‘grape‘,‘5.3‘),
  11. -> (‘o2‘,103,‘coconut‘,‘9.2‘),
  12. -> (‘c0‘,101,‘cherry‘,‘3.2‘),
  13. -> (‘a2‘,103,‘apricot‘,‘2.2‘),
  14. -> (‘l2‘,104,‘lemon‘,‘6.4‘),
  15. -> (‘b2‘,104,‘berry‘,‘7.6‘),
  16. -> (‘m1‘,106,‘mango‘,‘15.7‘),
  17. -> (‘m2‘,105,‘xbabay‘,‘2.6‘),
  18. -> (‘t4‘,107,‘xbababa‘,‘2.6‘),
  19. -> (‘m3‘,105,‘xxtt‘,‘11.6‘),
  20. -> (‘b5‘,107,‘xxxx‘,‘3.6‘);
  21. #创建第二个表
  22. mysql> create table customers(
  23. -> c_id int not null auto_increment,
  24. -> c_name char(50) not null,
  25. -> c_address char(50) null,
  26. -> c_city char(50) null,
  27. -> c_zip char(50) null,
  28. -> c_contact char(50) null,
  29. -> c_email char(50) null,
  30. -> primary key(c_id)
  31. -> );
  32. #第二张表插入数据
  33. mysql> insert into customers(c_id,c_name,c_address,c_city,c_zip,c_contact,c_email)
  34. -> values(10001,‘RedHook‘,‘200 Street‘,‘Tianjin‘,‘300000‘,‘LiMing‘,‘LMing@163.com‘),
  35. -> (1002,‘Stars‘,‘333 Fromage Lane‘,‘Dalian‘,‘116000‘,‘Zhangbo‘,‘Jerry@hotnail.com‘),
  36. -> (10003,‘Netbhood‘,‘1 Sunny Place‘,‘Qingdao‘,‘266000‘,‘LuoCong‘,NULL),
  37. -> (1004,‘JOTO‘,‘829 Riverside Drive‘,‘Haikou‘,‘570000‘,‘YangShan‘,‘sam@hotmail.com‘);</code>

2、查看两个表的所有数据
技术图片
技术图片
3、查询fruits表中f_name列的数据

  1. <code>
  2. mysql> select f_name from fruits;</code>

技术图片
4、查询fruits表中f_name和f_price两列的数据

  1. <code>
  2. mysql> select f_name,f_price from fruits;</code>

技术图片
5、查询fruits表中f_name和f_price的列,并且f_price的数值等于5.2

  1. <code>
  2. mysql> select f_name,f_price from fruits where f_price=5.2;</code>

技术图片
6、查询fruits表中f_name和f_price的列,并且f_price的数值大于或等于10

  1. <code>
  2. mysql> select f_name,f_price from fruits where f_price >= 10;</code>

技术图片
7、查询fruits表中f_name和f_price的列,并且f_price的数值在2到8之间

  1. <code>
  2. mysql> select f_name,f_price from fruits where f_price between 2 and 8;</code>

技术图片
8、查询fruits表中的f_name和s_id列,并且s_id的值为101或者103

  1. <code>方法不同,结果都一样
  2. 查询方法一:
  3. mysql> select f_name,s_id from fruits where s_id = 101 or s_id = 103;</code>

技术图片

  1. <code>查询方法二:
  2. mysql> select f_name,s_id from fruits where s_id in(101,103);</code>

技术图片
9、查询fruits表中的f_name和s_id列,并且s_id的值不为101或者103
查询方法一

  1. <code>
  2. mysql> select f_name,s_id from fruits where s_id != 101 and s_id != 103;</code>

技术图片
查询方法二

  1. <code>
  2. mysql> select f_name,s_id from fruits where s_id not in(101,103);</code>

技术图片
10、模糊查询“%”和“_”的使用

  1. <code>#查询fruits表中的f_name列,并且值以“b”开头
  2. mysql> select f_name from fruits where f_name like ‘b%‘;</code>

技术图片

  1. <code>#查询fruits表中的f_name列,并且值以“b”开头,以“y”结尾
  2. mysql> select f_name from fruits where f_name like ‘b%y‘;</code>

技术图片

  1. <code>#查询fruits表中的f_name列,值以“b”开头,以“y”结尾,并且b和y之间有三个字符
  2. mysql> select f_name from fruits where f_name like ‘b___y‘;</code>

技术图片
11、查询fruits表中s_id的值为101并且f_price的值大于2.0的行

  1. <code>
  2. mysql> select * from fruits where s_id = 101 and f_price > ‘2.0‘;</code>

技术图片
12、查询fruits表中s_id的值为101或103并且f_price列的值大于5

  1. <code>
  2. mysql> select * from fruits where s_id in(101,103) and f_price > 5;</code>

技术图片
13、查询fruits表中的s_id列,并去除重复值

  1. <code>
  2. mysql> select distinct s_id from fruits;</code>

技术图片
14、查询fruits表中的s_id和f_name列,并以s_id对结果进行排序

  1. <code>
  2. mysql> select s_id,f_name from fruits order by s_id;</code>

技术图片

15、查询fruits表中的f_name及f_price列,并以f_name和f_price列进行排序

  1. <code>
  2. mysql> select f_name,f_price from fruits order by f_name,f_price;</code>

技术图片
注:多字段排序,如果第一个排序的字段一致,会依靠第二个字段排序,依次类推,如果第一个字段不一样,则直接以第一段来进行排序。
16、查询fruits表中的f_price列,并对结果以降序进行排序

  1. <code>#默认是asc升序排序,可以通过关键字DESC更改为降序
  2. mysql> select f_price from fruits order by f_price desc;</code>

技术图片
17、查询fruits中s_id列不同值出现的次数,并对其进行分组显示

  1. <code>
  2. #调用count(*)函数统计次数,并通过as来对其设置别名,group by来进行分组
  3. mysql> select s_id,count(*) as total from fruits group by s_id;</code>

技术图片
18、查询fruits表中每个相同的s_id对应的f_name列的所有值,f_name的值以一行显示,并且其值在1个以上

  1. <code>
  2. mysql> select s_id,group_concat(f_name) as name from fruits group by s_id having count(f_name) > 1;</code>

技术图片
19、查询customers表中c_email列为空值的行

  1. <code>
  2. mysql> select * from customers where c_email is null;</code>

技术图片

MySQL数据库基本查询(一)

标签:开头   dal   出现   数据   not   email   调用   dao   net   

人气教程排行