在MySQL数据库中查询数据通用的 SELECT 语法:
查询语句中你可以使用一个或者多个表,表之间使用逗号(,)分割,并使用WHERE语句来设定查询条件。
SELECT 命令可以读取一条或者多条记录。
使用星号(*)来代替其他字段,SELECT语句会返回表的所有字段数据
使用 WHERE 语句来包含任何条件。
使用 LIMIT 属性来设定返回的记录数。
通过OFFSET指定SELECT语句开始查询的数据偏移量。默认情况下偏移量为0。
1、创建所需表及插入数据
#创建库
mysql> create database zjz;
#进入库
mysql> use zjz;
#创建数据表
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) );
#插入数据
mysql> insert into fruits(f_id,s_id,f_name,f_price)
-> values('a1',101,'apple','5.2'),
-> ('b1',101,'blackberry','10.2'),
-> ('bs1',102,'orange','11.2'),
-> ('bs2',105,'melon','8.2'),
-> ('t1',102,'banana','10.3'),
-> ('t2',102,'grape','5.3'),
-> ('o2',103,'coconut','9.2'),
-> ('c0',101,'cherry','3.2'),
-> ('a2',103,'apricot','2.2'),
-> ('l2',104,'lemon','6.4'),
-> ('b2',104,'berry','7.6'),
-> ('m1',106,'mango','15.7'),
-> ('m2',105,'xbabay','2.6'),
-> ('t4',107,'xbababa','2.6'),
-> ('m3',105,'xxtt','11.6'),
-> ('b5',107,'xxxx','3.6');
#创建第二个表
mysql> create table customers(
-> c_id int not null auto_increment,
-> c_name char(50) not null,
-> c_address char(50) null,
-> c_city char(50) null,
-> c_zip char(50) null,
-> c_contact char(50) null,
-> c_email char(50) null,
-> primary key(c_id)
-> );
#第二张表插入数据
mysql> insert into customers(c_id,c_name,c_address,c_city,c_zip,c_contact,c_email)
-> values(10001,'RedHook','200 Street','Tianjin','300000','LiMing','LMing@163.com'),
-> (1002,'Stars','333 Fromage Lane','Dalian','116000','Zhangbo','Jerry@hotnail.com'),
-> (10003,'Netbhood','1 Sunny Place','Qingdao','266000','LuoCong',NULL),
-> (1004,'JOTO','829 Riverside Drive','Haikou','570000','YangShan','sam@hotmail.com');
2、查看两个表的所有数据
3、查询fruits表中f_name列的数据
4、查询fruits表中f_name和f_price两列的数据