Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the SQL Where clause

2025-02-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to write SQL Where clauses". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to write SQL Where clauses".

SQL WHERE clause syntax

You write the WHERE clause like this:

SELECT column1, column2...FROM table_nameWHERE condition

Note that I wrote it here using the select statement, but its use is not limited to SELECT. You can also use it with other statements, such as DELETE and UPDATE.

Demo database

Let's use this users table as an example of how to use the WHERE clause.

Example of IDNAMEAGESTATEEMAIL1Brian15Michiganbrian@example.com2Leonard55Mississippileonard@example.com3Anvil31South Dakotaanvil@example.com4Jo44Mainejo@example.com5Meredith43Delawaremeredith@example.com6Cody16Michigancody@example.com7Dilara50Ohiodilara@example.com8Corbin47Wisconsincorbin@example.com9Gin63Illinoisgin@example.com10Alice50Nevadaalice@example.com11Zachary21Massachusettszachery@example.com12Delmar56Idahodelmar@example.com13Dennie96Ohiodennie@example.com14Aaron50Floridaaaron@example.com15Busrah18South Dakotabusrah@example.com16Aveline88Nevadaaveline@example.com17Aherin72Arkansasaherin@example.com18Viola66Maineviola@example.com19Nadya22Floridanadya@example.com20Izabela61Arizonaizabela@example.comSELECT SQL clause with WHERE statement

When you want to ensure that an event affects people 50 or older, you can select only users with the following code:

SELECT * FROM usersWHERE age > = 50

This will give you the following table, listing only users aged 50 or older:

Example of IDNAMEAGESTATEEMAIL2Leonard55Mississippileonard@example.com7Dilara50Ohiodilara@example.com9Gin63Illinoisgin@example.com10Alice50Nevadaalice@example.com12Delmar56Idahodelmar@example.com13Dennie96Ohiodennie@example.com14Aaron50Floridaaaron@example.com16Aveline88Nevadaaveline@example.com17Aherin72Arkansasaherin@example.com18Viola66Maineviola@example.com20Izabela61Arizonaizabela@example.comDELETE SQL clause with WHERE statement

Suppose Cody has decided to remove itself from the list. You can update the table, WHERE, with a DELETE statement to ensure that only Cody records are deleted.

DELETE FROM usersWHERE name IS "Cody"

The users looks like the following, without line 6 (in which Cody's message is):

IDNAMEAGESTATEEMAIL1Brian15Michiganbrian@example.com2Leonard55Mississippileonard@example.com3Anvil31South Dakotaanvil@example.com4Jo44Mainejo@example.com5Meredith43Delawaremeredith@example.com7Dilara50Ohiodilara@example.com8Corbin47Wisconsincorbin@example.com9Gin63Illinoisgin@example.com10Alice50Nevadaalice@example.com11Zachary21Massachusettszachery@example.com12Delmar56Idahodelmar@example.com13Dennie96Ohiodennie@example.com14Aaron50Floridaaaron@example.com15Busrah18South Dakotabusrah@example.com16Aveline88Nevadaaveline@example.com17Aherin72Arkansasaherin@example.com18Viola66Maineviola@example.com19Nadya22Floridanadya@example.com20Izabela61Arizonaizabela@example.com now you may have been informed that Anvil is old and is now 32 years old. You can use this UPDATE statement to change the record of Anvil, and you can use it to WHERE to ensure that only the record of Anvil is updated. Example SQL clause of UPDATE with WHERE statement UPDATE usersSET age = 32WHERE name IS "Anvil"

The table will now look like this:

IDNAMEAGESTATEEMAIL1Brian15Michiganbrian@example.com2Leonard55Mississippileonard@example.com3Anvil32South Dakotaanvil@example.com4Jo44Mainejo@example.com5Meredith43Delawaremeredith@example.com7Dilara50Ohiodilara@example.com8Corbin47Wisconsincorbin@example.com9Gin63Illinoisgin@example.com10Alice50Nevadaalice@example.com11Zachary21Massachusettszachery@example.com12Delmar56Idahodelmar@example.com13Dennie96Ohiodennie@example.com14Aaron50Floridaaaron@example.com15Busrah18South Dakotabusrah@example.com16Aveline88Nevadaaveline@example.com17Aherin72Arkansasaherin@example.com18Viola66Maineviola@example.com19Nadya22Floridanadya@example.com20Izabela61Arizonaizabela@example.com

Operators that can be used with the WHERE clause to select records

You can see the effect of "greater than or equal to" in the above example using =, >, =, =.

= is "equal", > "greater than", 70 will select users whose names begin with An or are over 70 years old (only one of the two parts must be true, but both can also be true).

There are 6 user names that start with An or are over 70 (or both).

It is important that IDNAMEAGESTATEEMAIL3Anvil32South Dakotaanvil@example.com10Alice50Nevadaalice@example.com13Dennie96Ohiodennie@example.com14Aaron50Floridaaaron@example.com16Aveline88Nevadaaveline@example.com17Aherin72Arkansasaherin@example.com specifies the records to be manipulated in the table. Thank you for your reading, the above is the content of "how to write SQL Where clause". After the study of this article, I believe you have a deeper understanding of how to write SQL Where clause, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report