search
HomeDatabaseMysql Tutorial数据库查询语句关键字总结

数据库查询语句关键字总结

Jun 07, 2016 pm 04:02 PM
KeywordsSummarizedatabaseInquirevideostatement

看过牛腩视频后,前17集讲后台代码的编写,尤其讲到查询语句的时候,顿时感觉原来学习过的查询语句中用到的关键字烟消云散了,啥都不记得了,通过看视频,帮着回忆了一部分,在这里总结一下,查询语句中用到的关键字的含义及使用。 一、select语句(单个表)

看过牛腩视频后,前17集讲后台代码的编写,尤其讲到查询语句的时候,顿时感觉原来学习过的查询语句中用到的关键字烟消云散了,啥都不记得了,通过看视频,帮着回忆了一部分,在这里总结一下,查询语句中用到的关键字的含义及使用。

一、select语句(单个表)

1.最简单的查询:

select * from [where ]

select column1,column2....from [where]

这里需要注意的是where子句中条件过滤使用到的关键字,比如用到逻辑运算符like 中的’%‘(匹配一个或多个字符)和’_‘(仅匹配一个)等。这个在新闻发布系统中也有用到。

例如:按标题搜索:

Select top 10 n.id,n.title,n.createtime,c.[name]   
from news n inner join category c on c.caid=c.id   
where n.title like '%' + @title + '%' 

当然还有很多,例如between,not ,in等关键字的使用也很重要。

2.DISTINCT关键字

这个关键字,主要用来取出列中唯一的值,比如:记录中的一个字段值(city)如果有重复(廊坊,北京,廊坊,北京),那么利用DISTINCT关键字取出唯一值,即任何重复的值只计数一次,结果为为:(廊坊,北京)。

select DISTINCT city from [table]  

3.使用别名

利用别名可以显示我们想要的名字,方便阅读。select city as 城市 from ...

4.group by 和having子句

group by 用来对查询到的结果集进行分组,必须位于select语句中的from子句或where子句之后。

having子句类似于where子句,紧跟在group by子后,作为一个查询条件。

与where子句的区别:where子句作用于一条记录中的查询条件,而having子句则作用于一列的查询条件

例如:

select  location from citytable  where city='北京'  --查询城市名为‘北京’的城市的位置 
select city group by city having count(memberId)>=3 --查询城市成员总数大于等于3的城市,同时按城市名分组  

二、多表查询

1、inner join

要求,查询的多张表中必须具有相同的匹配项。其中on表示作用的表的条件,n,c 为别名

    Select *  
    From news n  
    Inner join category c  
     on c.caid=c.id  

要执行的查询结果必须是在两张表中同时含有相同的类别号的记录才会被查询出来。

例如:以牛腩视频中例子为例:

category表中id表示新闻类别的id ,而news表中的caid则表示该新闻属于具体哪个类别

\

\

那么执行上面查询语句后的结果:

inner join表

\

可以看到结果为类别号在两张表中均存在的项。inner join还包括等值联合和不等值,这主要由on后面的条件决定

2.left join

左外连接:连接时,on条件左边表所有项均查询出来,而右边表中若无匹配项,则以null代替

上面两张表,执行

    select * from category c left join news n on  c.id=n.caid  

结果为:

\

3.right join

顾名思义,右外连接结果与left join相反,将右边表所有项查询出来,而左边表中无匹配项的则以null代替。

4.full join

无论左边还是右边所有项均返回结果。无对应项以null代替。

三、其它

除了以上涉及到的查询关键字外,还涉及到了嵌套查询,in关键字的使用,对sql记录进行编号排序后按顺序查询等。利用

    SELECT ROW_NUMBER() OVER (ORDER BY id desc)AS Row --Row为别名
以上只是涉及到了一部分的查询关键字的总结,也是经常用到的,逐渐学习,才发现原来的知识还是应该不断的回忆和应用才能发现它更深一层应用的含义。更多关于数据库的知识有待进一步实践总结。
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What are some tools you can use to monitor MySQL performance?What are some tools you can use to monitor MySQL performance?Apr 23, 2025 am 12:21 AM

How to effectively monitor MySQL performance? Use tools such as mysqladmin, SHOWGLOBALSTATUS, PerconaMonitoring and Management (PMM), and MySQL EnterpriseMonitor. 1. Use mysqladmin to view the number of connections. 2. Use SHOWGLOBALSTATUS to view the query number. 3.PMM provides detailed performance data and graphical interface. 4.MySQLEnterpriseMonitor provides rich monitoring functions and alarm mechanisms.

How does MySQL differ from SQL Server?How does MySQL differ from SQL Server?Apr 23, 2025 am 12:20 AM

The difference between MySQL and SQLServer is: 1) MySQL is open source and suitable for web and embedded systems, 2) SQLServer is a commercial product of Microsoft and is suitable for enterprise-level applications. There are significant differences between the two in storage engine, performance optimization and application scenarios. When choosing, you need to consider project size and future scalability.

In what scenarios might you choose SQL Server over MySQL?In what scenarios might you choose SQL Server over MySQL?Apr 23, 2025 am 12:20 AM

In enterprise-level application scenarios that require high availability, advanced security and good integration, SQLServer should be chosen instead of MySQL. 1) SQLServer provides enterprise-level features such as high availability and advanced security. 2) It is closely integrated with Microsoft ecosystems such as VisualStudio and PowerBI. 3) SQLServer performs excellent in performance optimization and supports memory-optimized tables and column storage indexes.

How does MySQL handle character sets and collations?How does MySQL handle character sets and collations?Apr 23, 2025 am 12:19 AM

MySQLmanagescharactersetsandcollationsbyusingUTF-8asthedefault,allowingconfigurationatdatabase,table,andcolumnlevels,andrequiringcarefulalignmenttoavoidmismatches.1)Setdefaultcharactersetandcollationforadatabase.2)Configurecharactersetandcollationfor

What are triggers in MySQL?What are triggers in MySQL?Apr 23, 2025 am 12:11 AM

A MySQL trigger is an automatically executed stored procedure associated with a table that is used to perform a series of operations when a specific data operation is performed. 1) Trigger definition and function: used for data verification, logging, etc. 2) Working principle: It is divided into BEFORE and AFTER, and supports row-level triggering. 3) Example of use: Can be used to record salary changes or update inventory. 4) Debugging skills: Use SHOWTRIGGERS and SHOWCREATETRIGGER commands. 5) Performance optimization: Avoid complex operations, use indexes, and manage transactions.

How do you create and manage user accounts in MySQL?How do you create and manage user accounts in MySQL?Apr 22, 2025 pm 06:05 PM

The steps to create and manage user accounts in MySQL are as follows: 1. Create a user: Use CREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password'; 2. Assign permissions: Use GRANTSELECT, INSERT, UPDATEONmydatabase.TO'newuser'@'localhost'; 3. Fix permission error: Use REVOKEALLPRIVILEGESONmydatabase.FROM'newuser'@'localhost'; then reassign permissions; 4. Optimization permissions: Use SHOWGRA

How does MySQL differ from Oracle?How does MySQL differ from Oracle?Apr 22, 2025 pm 05:57 PM

MySQL is suitable for rapid development and small and medium-sized applications, while Oracle is suitable for large enterprises and high availability needs. 1) MySQL is open source and easy to use, suitable for web applications and small and medium-sized enterprises. 2) Oracle is powerful and suitable for large enterprises and government agencies. 3) MySQL supports a variety of storage engines, and Oracle provides rich enterprise-level functions.

What are the disadvantages of using MySQL compared to other relational databases?What are the disadvantages of using MySQL compared to other relational databases?Apr 22, 2025 pm 05:49 PM

The disadvantages of MySQL compared to other relational databases include: 1. Performance issues: You may encounter bottlenecks when processing large-scale data, and PostgreSQL performs better in complex queries and big data processing. 2. Scalability: The horizontal scaling ability is not as good as Google Spanner and Amazon Aurora. 3. Functional limitations: Not as good as PostgreSQL and Oracle in advanced functions, some functions require more custom code and maintenance.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),