美洲杯外围下注_美洲杯投注导航_[所有网址]

美洲杯外围下注_美洲杯投注导航_[所有网址]是该公司为打造信誉第一,2014最新体验金,美洲杯外围下注_美洲杯投注导航_[所有网址]这里的所有老虎机游戏及真人百家乐游戏都具有欧洲风格,致力成为最具传播力和互动性的娱乐城网站。

查询数据

2019-07-17 02:32 来源:未知

本文实例总结了MySQL单表查询常见操作。分享给大家供大家参考,具体如下:

美洲杯竞猜网址,一. 基本查询语句

本文实例讲述了MySQL Union合并查询数据及表别名、字段别名用法。分享给大家供大家参考,具体如下:

创建fruits表:

mysql从数据表中查询数据的基本语句为select语句。基本格式为:

union关键字

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 < 表1 > < 表2  >... 

[ WHERE < 表达式 > ]

[ GROUP BY < group by definition > ]

[ HAVING < expression >  [ { < operator > < expression > } ... ] ]

[ ORDER BY < order by definition > ]

[ ORDER  BY < order by difinition > ]

[ Limit  [ < offset > ] <row count> ]

]

SELECT s_id, f_name, f_price
FROM fruits
WHERE f_price < 9.0
UNION ALL
SELECT s_id, f_name, f_price
FROM fruits
WHERE s_id IN(101,103);

常用查询:

二. 单表查询

为表取别名

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";

单表查询是指从一张表数据中查询所需的数据。

SELECT * from orders AS o
WHERE o.o_num = 30001;



SELECT c.c_id, o.o_num
FROM customers AS c LEFT OUTER JOIN orders AS o
ON c.c_id = o.c_id;

创建customers表:

1.查询所有字段

为字段取别名

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 查询记录最简单的形式是从一个表中检索所有记录,实现方法是使用(*)通配符指定查找所有列的名称。

SELECT f1.f_name AS fruit_name, f1.f_price AS fruit_price
FROM fruits AS f1
WHERE f1.f_price < 8;



SELECT CONCAT(RTRIM(s_name) , ' (', RTRIM(s_city), ')')
FROM suppliers
ORDER BY s_name;



SELECT CONCAT(RTRIM(s_name) , ' (', RTRIM(s_city), ')')
as suppliers_title
FROM suppliers
ORDER BY s_name;

常用查询语句:

SELECT * FROM 表名;

【例.62】查询所有价格小于9的水果的列表,查询s_id等于101和103所有的水果种类,使用UNION连接查询结果

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;

2.查询指定字段

SELECT s_id, f_name, f_price
FROM fruits
WHERE f_price < 9.0
UNION ALL
SELECT s_id, f_name, f_price
FROM fruits
WHERE s_id IN(101,103);

使用in操作更加简洁明了

1)查询单个字段

【例.63】查询所有价格小于9的水果的列表,查询s_id等于101和103所有的水果种类,使用UNION ALL连接查询结果,SQL语句如下

select f_name,f_price
from fruits
where s_id in (101 ,102);

查询表中一个字段,语法如下:

SELECT s_id, f_name, f_price
FROM fruits
WHERE f_price < 9.0
UNION ALL
SELECT s_id, f_name, f_price
FROM fruits
WHERE s_id IN(101,103);

字段不重复

SELECT 列名 FROM 表名;

【例.64】为orders表取别名o,查询订30001订单的下单日期

SELECT DISTINCT s_id FROM fruits;
select f_name from fruits ORDER BY f_name;

2)查询多个字段

SELECT * from orders AS o
WHERE o.o_num = 30001;

如果第一列数据中没有相同值,将不再对第二列进行排序。

使用select声明,可以获取多个字段下的数据,只需要在关键字select后面指定要查找的字段名称,不同字段名称之间用逗号(,)分隔开,最后一个字段后面不需要加逗号,语法格式如下:

【例.65】为customers和orders表分别取别名,并进行连接查询

SELECT f_name, f_price
FROM fruits
ORDER BY f_name, f_price;

SELECT 字段名1,字段名2,...,字段名n  FROM  表名;

SELECT c.c_id, o.o_num
FROM customers AS c LEFT OUTER JOIN orders AS o
ON c.c_id = o.c_id;

按价格降序排列,desc为降序,默认为升序。

3.查询指定记录

【例.66】查询fruits表,为f_name取别名fruit_name,f_price取别名fruit_price,为fruits表取别名f1,查询表中f_price < 8的水果的名称

SELECT f_name, f_price FROM fruits ORDER BY f_price desc;
SELECT f_name, f_price FROM fruits ORDER BY f_price desc,f_name;
SELECT s_id, COUNT(*) AS Total FROM fruits GROUP BY s_id;
SELECT s_id, GROUP_CONCAT(f_name) AS Names FROM fruits GROUP BY s_id;

数据库中包含大量的数据,根据特殊要求,可能只需要查询表中的指定数据,即对数据进行过滤。在select语句中,通过where子句可对数据进行过滤,语法格式如下:

SELECT f1.f_name AS fruit_name, f1.f_price AS fruit_price
FROM fruits AS f1
WHERE f1.f_price < 8;

使用having过滤分组

SELECT 字段名1,字段名2,...,字段名n

FROM 表名

WHERE 查询条件

【例.67】查询suppliers表中字段s_name和s_city,使用CONCAT函数连接这个两个字段值,并取列别名为suppliers_title

SELECT s_id, GROUP_CONCAT(f_name) AS Names
FROM fruits
GROUP BY s_id having count(f_name)>1;

在WHERE子句中,mysql提供了一系列的条件判断符:=,!=,<,<=,,>,>=,BETWEEN( 介于两者之间 。)

如果没有对连接后的值取别名,其显示列名称将会不够直观,输入如下SQL

group by 子句中使用with rollup

4.带IN关键字的查询

SELECT CONCAT(RTRIM(s_name) , ' (', RTRIM(s_city), ')')
FROM suppliers
ORDER BY s_name;
SELECT s_id, COUNT(*) AS Total FROM fruits GROUP BY s_id WITH ROLLUP;
SELECT * from fruits group by s_id,f_name;

IN操作符用来查询满足指定范围内的条件的记录,使用IN操作符,将所有检索条件用括号括起来,检索条件之间用逗号隔开,只要满足条件范围内的一个值即为匹配项。

更多关于MySQL相关内容感兴趣的读者可查看本站专题:《MySQL常用函数大汇总》、《MySQL日志操作技巧大全》、《MySQL事务操作技巧汇总》、《MySQL存储过程技巧大全》及《MySQL数据库锁相关技巧汇总》

创建orderitems表:

查询s_id为101和102的记录,sql语句如下:

希望本文所述对大家MySQL数据库计有所帮助。

CREATE TABLE orderitems
(
 o_num   int     NOT NULL,
 o_item   int     NOT NULL,
 f_id    char(10)   NOT NULL,
 quantity  int     NOT NULL,
 item_price decimal(8,2) NOT NULL,
 PRIMARY KEY (o_num,o_item)
) ;
INSERT INTO orderitems(o_num, o_item, f_id, quantity, item_price)
VALUES(30001, 1, 'a1', 10, 5.2),
(30001, 2, 'b2', 3, 7.6),
(30001, 3, 'bs1', 5, 11.2),
(30001, 4, 'bs2', 15, 9.2),
(30002, 1, 'b3', 2, 20.0),
(30003, 1, 'c0', 100, 10),
(30004, 1, 'o2', 50, 2.50),
(30005, 1, 'c0', 5, 10),
(30005, 2, 'b1', 10, 8.99),
(30005, 3, 'a2', 10, 2.2),
(30005, 4, 'm1', 5, 14.99);

SELECT s_id FROM student WHERE s_id IN(101,102);

您可能感兴趣的文章:

  • Mysql联合查询UNION和UNION ALL的使用介绍
  • MySQL中union和order by同时使用的实现方法
  • Mysql联合查询UNION和Order by同时使用报错问题的解决办法
  • MySQL union 语法代码示例分析
  • 浅析mysql union和union all
  • MySQL中使用表别名与字段别名的基本教程
  • 关于sql和mysql对于别名不能调用的一些理解
  • MySQL笔记之别名的使用

常用查询语句:

相反的,可以使用关键字NOT来检索不在条件范围内的记录。

SELECT o_num, SUM(quantity * item_price) AS orderTotal
FROM orderitems
GROUP BY o_num
HAVING SUM(quantity*item_price) >= 100;
SELECT o_num, SUM(quantity * item_price) AS orderTotal
FROM orderitems
GROUP BY o_num
HAVING SUM(quantity*item_price) >= 100
order by ordertotal;
SELECT * From fruits LIMIT 8;

SELECT s_id FROM student s_id NOT IN(101,101);

从第五行开始,读取3行

5.带BETWEEN AND的范围查询

SELECT * From fruits LIMIT 4,3;

BETWEEN AND 用来查询某个范围内的值,该操作需要两个参数,即范围开始值和结束值,如果字段满足指定的范围查询条件,则这些记录被返回。

【例.1】从fruits表中检索所有字段的数据

查询学号在1到5之间的学生姓名,sql语句如下:

SELECT * FROM fruits;
SELECT f_id, s_id ,f_name, f_price FROM fruits;

SELECT NAME FROM student WHERE id BETWEEN 1 AND 5;

【例.2】查询当前表中f_name列所有水果名称,输入如下语句:

6.带LIKE的字符匹配查询

SELECT f_name FROM fruits;

1)百分号通配符‘%’,匹配任意长度的字符,甚至包括零字符。

【例.3】例如,从fruits表中获取f_name和f_price两列,输入如下语句:

查找所有以‘b’字母开头的水果,sql语句如下:

SELECT f_name, f_price FROM fruits;

SELECT f_id,f_name FROM friuits WHERE f_name LIKE 'b%';

TAG标签:
版权声明:本文由美洲杯外围下注发布于系统,转载请注明出处:查询数据