Introduction to SQL

目录

  • SELECTING

    • SELECTing single columns
    • SELECTing multiple columns
    • select all
    • SELECT DISTINCT
    • Learning to COUNT
  • FILTERING
    • WHERE
    • Simple filtering of text
    • WHERE AND
    • WHERE AND OR
    • BETWEEN

SELECTING

SELECTing single columns

格式:
select xx from xx
SELECT不区分大小写

select name from people;

SELECTing multiple columns

格式:
select A,B,C.. from xx;

SELECT title, release_year, country
FROM films;

select all

选择数据库里全部的列

SELECT *
FROM films;

SELECT DISTINCT

often your results will include many duplicate values. If you want to select all the unique values from a column, you can use the DISTINCT keyword.

SELECT DISTINCT language
FROM films;

Learning to COUNT

统计“记录”
What if you want to count the number of employees in your employees table? The COUNT statement lets you do this by returning the number of rows in one or more columns.

统计所有记录用*

How many records are contained in the reviews table?
SELECT COUNT(*)
FROM xx;

统计某项记录有多少条
SELECT COUNT(xx)
FROM xx;

SELECT COUNT(birthdate)
FROM people;

COUNT和DISTINCT搭配使用效果更佳,统计不重复的记录

Count the number of unique birth dates in the people table.
SELECT COUNT(DISTINCT birthdate)
FROM people;

FILTERING

WHERE

换句话说,按条件筛选,但是条件要后写
In SQL, the WHERE keyword allows you to filter based on both text and numeric values in a table. There are a few different comparison operators you can use:

  • = equal
  • <> not equal
  • < less than
  • /> greater than 就是大于号,加上一个转义字符/
  • <= less than or equal to
  • />= greater than or equal to
SELECT *
FROM films
WHERE budget > 10000; 看这里,条件要后写
select title, release_year from films
where release_year>2000

Simple filtering of text

Remember, the WHERE clause can also be used to filter text results, such as names or countries.

SELECT title
FROM films
WHERE country = 'China';

注意就是一定要用单引号

select * from films
where certification = 'R';

WHERE AND

Often, you‘ll want to select data based on multiple conditions. You can build up your WHERE queries by combining multiple conditions with the AND keyword.

SELECT title
FROM films
WHERE release_year > 1994 AND < 2000;
select * from films
where language='Spanish' and release_year > 2000 AND release_year< 2010;

上面这句这么写可能比较麻烦了,回头改下

WHERE AND OR

What if you want to select rows based on multiple conditions where some but not all of the conditions need to be met? For this, SQL has the OR operator.

SELECT title
FROM films
WHERE (release_year = 1994 OR release_year = 1995)
AND (certification = 'PG' OR certification = 'R');
SELECT title, release_year
FROM films
WHERE (release_year >= 1990 AND release_year < 2000)
AND (language = 'French' OR language = 'Spanish')
AND gross > 2000000;

BETWEEN

位于。。。之间
As you‘ve learned, you can use the following query to get titles of all films released in and between 1994 and 2000:

SELECT title
FROM films
WHERE release_year >= 1994
AND release_year <= 2000;

Checking for ranges like this is very common, so in SQL the BETWEEN keyword provides a useful shorthand for filtering values within a specified range. This query is equivalent to the one above:

SELECT title
FROM films
WHERE release_year
BETWEEN 1994 AND 2000;

原文地址:https://www.cnblogs.com/gaowenxingxing/p/12080422.html

时间: 2024-11-02 05:35:55

Introduction to SQL的相关文章

《Pro SQL Server Internals, 2nd edition》的CHAPTER 3 Statistics中的Introduction to SQL Server Statistics、Statistics and Execution Plans、Statistics Maintenance(译)

<Pro SQL Server Internals> 作者: Dmitri Korotkevitch 出版社: Apress出版年: 2016-12-29页数: 804定价: USD 59.99装帧: PaperbackISBN: 9781484219638 统计 SQL Server查询优化器在为查询选择执行计划时使用基于成本的模型.它估计不同执行计划的成本,并选择成本最低的一个.但是,请记住,SQL Server并不搜索查询可用的最佳执行计划,因为评估所有可能的替代方案在CPU方面都是耗时

Stairway to SQL Server Replication: Level 1 - Introduction to SQL Server Replication翻译,合并截图翻译

SQL Server复制的阶梯:第1级 -  SQL Server复制简介 网址:http://www.sqlservercentral.com/articles/Stairway+Series/72274/ 作者: Sebastian Meine, 2012/12/26 该系列介绍:本文是Stairway系列文章的一部分:SQL Server复制的阶梯 SQL复制可以解决运行数据库驱动的应用程序时的许多问题.发布/订阅者的模型并不容易理解,脚本和监视复制系统的复杂性需要一些思考.最后,这一系列

16 SQL Tuning Overview

16.1 Introduction to SQL Tuning Identifying high load or top SQL statements that are responsible for a large share of the application workload and system resources, by reviewing past SQL execution history available in the system Verifying that the ex

通往t - sql的阶梯:超越基本级别6:使用CASE表达式和IIF函数

摘要:此文章转自: http://www.sqlservercentral.com/articles/Stairway+Series/108723/ Stairway to T-SQL: Beyond The Basics Level 6: Using the CASE Expression and IIF Function 通往t - sql的阶梯:超越基本级别6:使用CASE表达式和IIF函数 By Gregory Larsen, 2016/04/20 (first published: 2

第九篇 Replication:复制监视器

本篇文章是SQL Server Replication系列的第九篇,详细内容请参考原文. 复制监视器允许你查看复制配置组件的健康状况.这一篇假设你遵循前八篇,并且你已经有一个合并发布和事务发布.启动复制监控器复制监视器不是SSMS的一部分,它是一个独立的可执行文件(SqlMonitor.exe).在一个标准的SQL Server安装中开始菜单下找不到复制监视器.启动复制监视器的最简单方法是:SSMS对象资源管理器下连接到发布服务器,右击你的发布选择"启动复制监视器",如图9.1所示:图

利用OLEDB+SqlClient实现EXCEL批量导入数据

以下是几个自己写的类 /// <summary> /// 取得Excel对象 /// </summary> /// <param name="strConn">OLEDB连接字符串</param> /// <param name="sql">SQL语句</param> /// <returns></returns> public static DataTable GetE

数据访问的历史 Windows

节选:Programming Microsoft Visual Basic 6.0 1999 The Data Access Saga All the new database-related capacities in Visual Basic 6 are based on Microsoft ActiveX Data Objects (ADO), a technology that lets you access any database or data source, as long as

OCA,OCP,OCM傻傻分不清?

可能大家知道OCA.OCP.OCM的关系是一个比一个难考,一个比一个含金量高,但是你知道具体的考试科目.考试方式.就业形势区别吗?不知道的话这篇通俗易懂的文章会让你一目了然. 区别一:含金量 ■OCA:数据库专业人员踏上Oracle数据库认证之途的第一步 表示具备Oracle数据库管理的基础知识. ■OCP:数据库专业人员掌握Oracle专项技术的行业认可证明,证明持证者能够以最高效的方式建立和管理关键的Oracle数据库功能. ■OCM:针对在Oracle技术领域拥有多年实践经验,并且经过高级

Temporary Segments: What Happens When a Sort Occurs (文档 ID 102339.1)

APPLIES TO: Oracle Database - Enterprise Edition - Version 8.1.7.4 to 11.2.0.1 [Release 8.1.7 to 11.2]Information in this document applies to any platform.***Checked for relevance on 03-Nov-2014*** PURPOSE This article describes what happens when a s