数据库查询语句关键字总结
看过牛腩视频后,前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为别名

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



1. First open Weibo on your mobile phone and click [Me] in the lower right corner (as shown in the picture). 2. Then click [Gear] in the upper right corner to open settings (as shown in the picture). 3. Then find and open [General Settings] (as shown in the picture). 4. Then enter the [Video Follow] option (as shown in the picture). 5. Then open the [Video Upload Resolution] setting (as shown in the picture). 6. Finally, select [Original Image Quality] to avoid compression (as shown in the picture).

Download the latest version of 12306 ticket booking app. It is a travel ticket purchasing software that everyone is very satisfied with. It is very convenient to go wherever you want. There are many ticket sources provided in the software. You only need to pass real-name authentication to purchase tickets online. All users You can easily buy travel tickets and air tickets and enjoy different discounts. You can also start booking reservations in advance to grab tickets. You can book hotels or special car transfers. With it, you can go where you want to go and buy tickets with one click. Traveling is simpler and more convenient, making everyone's travel experience more comfortable. Now the editor details it online Provides 12306 users with a way to view historical ticket purchase records. 1. Open Railway 12306, click My in the lower right corner, and click My Order 2. Click Paid on the order page. 3. On the paid page

How to check my academic qualifications on Xuexin.com? You can check your academic qualifications on Xuexin.com, but many users don’t know how to check their academic qualifications on Xuexin.com. Next, the editor brings you a graphic tutorial on how to check your academic qualifications on Xuexin.com. Interested users come and take a look! Xuexin.com usage tutorial: How to check your academic qualifications on Xuexin.com 1. Xuexin.com entrance: https://www.chsi.com.cn/ 2. Website query: Step 1: Click on the Xuexin.com address above to enter the homepage Click [Education Query]; Step 2: On the latest webpage, click [Query] as shown by the arrow in the figure below; Step 3: Then click [Login Academic Credit File] on the new page; Step 4: On the login page Enter the information and click [Login];

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.
