本文实例总结了MySQL单表查询常见操作。,具体如下:
创建fruits表:
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)) ;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.6),('m2',105,'xbabay', 2.6),('t4',107,'xbababa', 3.6),('m3',105,'xxtt', 11.6),('b5',107,'xxxx', 3.6); |
常用查询:
SELECT * FROM fruits;select f_name,f_price from fruits;select f_name,f_price from fruits where f_price=10.2;select f_name,f_price from fruits where f_price<10;select * from fruits where s_id in (101,102) order by f_name;select * from fruits where s_id not in (101,102) order by f_name;select f_name,f_price from fruits where f_price between 2.00 and 10.20;select f_name,f_price from fruits where f_price not between 2.00 and 10.20;select f_name,f_price from fruits where f_name like "b%";select f_name,f_price from fruits where f_name like "%g%";select f_name,f_price from fruits where f_name like "b%y";select f_name,f_price from fruits where f_name like "____y"; |
创建customers表:
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(10) NULL, c_contact char(50) NULL, c_email char(255) NULL, PRIMARY KEY (c_id));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'),(10002, 'Stars', '333 Fromage Lane', 'Dalian', '116000', 'Zhangbo','Jerry@hotmail.com'),(10003, 'Netbhood', '1 Sunny Place', 'Qingdao', '266000', 'LuoCong', NULL),(10004, 'JOTO', '829 Riverside Drive', 'Haikou', '570000', 'YangShan', 'sam@hotmail.com'); |
常用查询语句:
select c_id,c_name,c_email from customers where c_email is null;select c_id,c_name,c_email from customers where c_email is not null;select f_name,f_price from fruits where s_id=101 and f_price>=5;select f_name,f_price from fruits where s_id in (101,102) and f_price >=5 and f_name="apple";select f_name,f_price from fruits where s_id=101 or s_id=102; |
使用in
操作更加简洁明了
select f_name,f_pricefrom fruitswhere s_id in (101 ,102); |
字段不重复
SELECT DISTINCT s_id FROM fruits;select f_name from fruits ORDER BY f_name; |
如果第一列数据中没有相同值,将不再对第二列进行排序。
新闻热点
疑难解答