sql查询前50条

In this article, we’ll go over the most common SQL queries that you should know to be able to use SQL efficiently.

在本文中,我们将介绍最常见SQL查询,您应该知道它们能够有效地使用SQL。

将覆盖哪些SQL查询? (What SQL Queries Will Be Covered?)

We’ll be going over queries that are used in the day-to-day situations of a database administrator. Commands by themselves will not really do anything unless they’re formed into a query. Let’s get right into this.

我们将遍历数据库管理员日常使用的查询。 除非将命令形成查询,否则命令本身不会真正执行任何操作。 让我们开始吧。

1.创建数据库和表 (1. Create Databases and Tables )

To start working with SQL tables, you’ll need to create them first. Tables reside within Databases and you can create both tables and databases using the CREATE keyword.

要开始使用SQL表,您需要首先创建它们。 表驻留在数据库中,您可以使用CREATE关键字创建表和数据库。

Let’s create a database, then enter into the database and then create a table.

让我们创建一个数据库,然后输入数据库,然后创建一个表。


CREATE db_name;
/* Use the specified database for executing further queries */
USE db_name;
CREATE TABLE authors (author_name VARCHAR(70), author_email VARCHAR(80), author_pay int);

2.将单值插入表中SQL查询 (2. SQL Queries to Insert Single Values into Tables)

After creating a table, it’s time to add data to the tables. Let’s add some data to our authors table.

创建表后,是时候向表中添加数据了。 让我们将一些数据添加到我们的authors表中。


INSERT INTO authors (author_name, author_email)
VALUES("Joe", "joe@journaldev.com", 50000);

4.将多个值插入表 (4. Insert Multiple Values into Tables)

If you have to insert multiple values, the above method is inefficient and adding them with a single query will be much better. You can insert multiple values with the below query.

如果必须插入多个值,则上述方法效率不高,并且通过单个查询添加它们会更好。 您可以使用以下查询插入多个值。


INSERT INTO authors (author_name, author_email)
VALUES ("Joe", "joe@journaldev.com", 50000),
("Jane", "jane@journaldev.com", 70000),
("John", "john@journaldev.com", 20000);

5. SQL查询以从表中检索所有数据 (5. SQL Queries to Retrieve All Data From A Table)

Now that you know how to create databases and tables and insert data into it, this query will help you display the data within the tables.

既然您知道如何创建数据库和表并将数据插入其中,此查询将帮助您在表中显示数据。

We use the select query to retrieve data from different tables. This is one of the queries that you’ll learn when you’re beginning with your SQL learning.

我们使用选择查询从不同的表中检索数据。 这是开始学习SQL时将学习的查询之一。


SELECT * FROM authors;

6.从表中检索特定列 (6. Retrieve Specific Columns From A Table)

The above command will retrieve everything from the table. But if you want to retrieve only a specific column, you can use the below query.

上面的命令将从表中检索所有内容。 但是,如果您只想检索特定的列,则可以使用以下查询。


SELECT author_name FROM authors;

7.使用WHERE关键字检索特定数据 (7. Retrieve Specific Data with WHERE Keyword)

If we use the * operator, the entire table is displayed. We can narrow down our results with the use of the WHERE keyword to display specific rows.

如果我们使用*运算符,则会显示整个表格。 我们可以使用WHERE关键字来显示特定行来缩小结果范围。

In the below query, I’ll extract the author with the email jane@journaldev.com.

在下面的查询中,我将使用电子邮件jane@journaldev.com提取作者。


SELECT * FROM authors WHERE author_email = "jane@journaldev.com";

8.更新单行SQL查询 (8. SQL Queries to Update Single Rows)

Inserted data may need updates and changes and we can use the UPDATE command to update any rows.

插入的数据可能需要更新和更改,我们可以使用UPDATE命令更新任何行。

With a combination of UPDATE, SET and WHERE commands, we can update data for specific rows.

结合使用UPDATE,SET和WHERE命令,我们可以更新特定行的数据。


UPDATE authors SET author_name="Jordan" where
author_email="joe@jornaldev.com";

9.更新多行 (9. Update Multiple Rows)

If we skip the WHERE keyword from the above command, we’ll be able to update all the rows in a specific table.

如果从上述命令中跳过WHERE关键字,我们将能够更新特定表中的所有行。


UPDATE authors SET author_name="Jordan";

The command will update all the author’s names to Jordan.

该命令会将所有作者的姓名更新为Jordan。

10.删除单行 (10. Delete Single Row)

You can delete single or multiple rows with the use of the DELETE command paired with the WHERE command.

您可以使用DELETE命令和WHERE命令配对来删除单行或多行。

Let’s delete the author with the email john@jornaldev.com.

让我们通过电子邮件john@jornaldev.com删除作者。


DELETE FROM authors WHERE author_email="john@journaldev.com";

11.删除多行 (11. Delete Multiple Rows)

To delete multiple rows of the table, you can enter multiple WHERE conditions using the boolean AND or OR.

要删除表的多行,可以使用布尔AND或OR输入多个WHERE条件。


DELETE FROM authors WHERE author_email="john@journaldev.com"
OR author_email="jane@journaldev.com";

12.计数行 (12. Counting Rows)

We can count rows using the COUNT keyword. This will print the count of the author’s emails.

我们可以使用COUNT关键字对行进行计数。 这将打印作者的电子邮件计数。


SELECT COUNT(author_email) FROM authors;

13.获取数据总和 (13. Get a Sum for Data)

Similar to how we used the COUNT keyword above, we can use the SUM keyword to get the total for a specific column. Let’s get the total pay for authors.

与我们上面使用COUNT关键字的方式类似,我们可以使用SUM关键字来获取特定列的总数。 让我们获取作者的总薪水。


SELECT SUM(author_pay) FROM authors;

14.获取数据平均值 (14. Get Average Values for Data)

Now that we know how to get the total, let’s get the average pay for our authors.

现在,我们知道了如何获得总金额,现在让我们获取作者的平均工资。


SELECT AVG(author_pay) FROM authors;

15.创建视图 (15. Creating Views)

Views are a very interesting feature of SQL. They’re like virtual tables that contain specific data that we’ve selected. We can manipulate and view data from those views

视图是SQL的一个非常有趣的功能。 它们就像虚拟表,其中包含我们选择的特定数据。 我们可以从这些视图中操纵和查看数据


CREATE VIEW high_pay_authors AS SELECT * FROM authors
WHERE author_pay > 50000;

This will create a virtual table named “high_pay_authors” which can be used as an independent table to view and manipulate data.

这将创建一个名为“ high_pay_authors”的虚拟表,该表可用作独立的表来查看和操作数据。

16.将列添加到表 (16. Add Columns to a Table)

Using the ALTER keyword, we can add columns to a table.

使用ALTER关键字,我们可以将列添加到表中。


ALTER TABLE authors ADD author_age int;

This will add an integer column for the author’s age.

这将为作者的年龄添加一个整数列。

17.从表中删除列 (17. Remove Column From a Table)

With the use of the DROP keyword, you can also remove columns from a table.

通过使用DROP关键字,您还可以从表中删除列。


ALTER TABLE authors DROP COLUMN author_age;

This will delete the column but make sure you have the required privileges to drop the columns.

这将删除该列,但请确保您具有删除这些列所需的特权。

18.用于查询值SQL查询 (18. SQL Queries to Search for Values )

We already looked at the WHERE keyword to search for specific values within a database table and return the row that has the specific value. But we don’t always know the exact value.

我们已经查看了WHERE关键字,以在数据库表中搜索特定值并返回具有特定值的行。 但是我们并不总是知道确切的值。

For such situations, we can use pattern matching with the LIKE keyword.

在这种情况下,我们可以将模式匹配与LIKE关键字一起使用。


SELECT * FROM authors WHERE author_name LIKE "j%"

This query will pattern match all the names that start with the letter “j” which in our case will return all the rows.

此查询将匹配所有以字母“ j”开头的名称,在本例中将返回所有行。

19.从列交换数据 (19. Swapping Data From Columns)

To switch the data between two columns, you can directly use the command below.

要在两列之间切换数据,可以直接使用下面的命令。


UPDATE authors SET author_email=author_name, author_name=author_email

This simple query will swap all the data from the author_email column to the author_name column and vice versa.

这个简单的查询会将所有数据从author_email列交换到author_name列,反之亦然。

20.重命名表 (20. Rename Tables )

Depending on your SQL version and privileges, you can use the below command to rename a table.

根据您SQL版本和特权,可以使用以下命令重命名表。


sp_RENAME authors authors_renamed;

This command will rename our “authors” table to “authors_renamed”.

此命令会将我们的“作者”表重命名为“ authors_renamed”。

21.从表中返回唯一值 (21. Return Unique Values from a Table)

In our table above, we have multiple people but all of the names and emails are unique. If we had a table with multiple rows had columns with the same values, we use the DISTINCT keyword to return only unique values from the tables.

在上面的表格中,我们有多个人,但是所有的名称和电子邮件都是唯一的。 如果我们的表中的多行具有相同的值的列,则可以使用DISTINCT关键字从表中仅返回唯一值。


SELECT DISTINCT author_name FROM authors;

22.子集表SQL查询 (22. SQL Queries for Subsetting Tables)

When working with larger databases, it only makes sense to view limited data at a time. The TOP command allows us to display only a specific number of rows from the table in the output.

当使用较大的数据库时,仅一次查看有限的数据才有意义。 TOP命令允许我们在输出中仅显示表中特定数量的行。


SELECT TOP 50 FROM authors;

23.备份数据库SQL查询 (23. SQL Queries to Backup Databases)

This is one of the SQL queries that you must get used to or at least create a script to automate backups because backups are very important.

这是您必须习惯SQL查询之一,或者至少创建一个脚本来自动执行备份,因为备份非常重要。


BACKUP DATABASE db_name
TO DISK = "/home/databases/db_name-backup.db";

The above command will backup the database db_name to a file named db_name-backup.db.

上面的命令会将数据库db_name备份到名为db_name-backup.db的文件中。

24.仅备份数据库的更新部分 (24. Backup Only Updated Part of Database)

Adding “DIFFERENTIAL” to the query will backup the “updated” parts or the parts that have changed since the last update. This reduces the time required to backup compared to a full backup

在查询中添加“ DIFFERENTIAL”将备份“更新的”零件或自上次更新以来已更改的零件。 与完全备份相比,这减少了备份所需的时间


BACKUP DATABASE db_name
TO DISK = "/home/databases/db_name-backup.bak"
WITH DIFFERENTIAL;

25.还原数据库备份 (25. Restore Database Backups)

We learned how to create backups for databases, now let’s learn to restore the backed-up file into our database.

我们学习了如何为数据库创建备份,现在让我们学习将备份文件还原到我们的数据库中。


RESTORE DATABASE db_name
TO DISK="/home/databases/db_name-backup.bak"

26.将查询结果复制到表中 (26. Copy Results of a Query into a Table)

We looked at the WHERE command to display the pay of the authors and figured which ones are highly paid. We also created a view that allowed us to create a virtual table to work with.

我们查看了WHERE命令以显示作者的薪水,并弄清哪些人的薪水很高。 我们还创建了一个视图,该视图允许我们创建要使用的虚拟表。

Now we’ll copy the entire set of data matching a specific query into another table that has the same columns.

现在,我们将与特定查询匹配的整个数据集复制到另一个具有相同列的表中。


INSERT INTO high_paid_authors
SELECT * FROM authors WHERE author_pay > 50000;

27.创建存储过程 (27. Creating Stored Procedures)

Stored procedures are SQL queries that can be run repeatedly. This saves you time when the queries are big. You can save them with a procedure name and then execute the procedure whenever required.

存储过程是可以重复运行SQL查询。 当查询很大时,这可以节省您的时间。 您可以使用过程名称保存它们,然后在需要时执行该过程。


CREATE PROCEDURE findAuthors
AS SELECT * FROM authors GO;

28.用于SQL查询的布尔运算符 (28. Boolean Operators for SQL Queries)

If you need to provide multiple conditions within an SQL query, it’s good to know the Boolean Operators.

如果您需要在SQL查询中提供多个条件,那么最好了解布尔运算符。


DELETE FROM authors WHERE author_email="john@journaldev.com"
OR author_email="jane@journaldev.com";

The above query will match either of the two emails and will return 2 rows. If we use the same query with the AND operator, it will return zero rows.

上面的查询将匹配两封电子邮件中的任何一封,并将返回2行。 如果我们对AND运算符使用相同的查询,它将返回零行。


DELETE FROM authors WHERE author_email="john@journaldev.com"
AND author_email="jane@journaldev.com";

This is because the AND operator requires both the conditions to be true while the OR operator works with either of the conditions being true.

这是因为AND运算符要求两个条件都为真,而OR运算符要求两个条件都为真。

29.查找范围之间的值 (29. Find Values Between a Range)

SQL provides a very easy to use BETWEEN keyword that helps us return the rows that have a value that’s between the specified range.

SQL提供了一个非常易于使用的BETWEEN关键字,它有助于我们返回值在指定范围之间的行。


SELECT *  FROM authors WHERE author_pay
BETWEEN 50000 AND 100000

30.否定SQL查询中的查询或表达式 (30. Negating Queries or Expressions in SQL Queries)

Similar to the boolean operators AND and OR, we have the NOT keyword which negates any expression that follows and returns a value that’s opposite.

与布尔运算符AND和OR相似,我们使用NOT关键字来否定后面的任何表达式并返回相反的值。


SELECT *  FROM authors WHERE author_pay
NOT BETWEEN 50000 AND 100000

In this demonstration, the values which are not between 50,000 and 100,000 will be returned.

在此演示中,将返回不介于50,000和100,000之间的值。

31.寻找最小值 (31. Finding Minimum Values)

SQL provides a very easy to use function to help find the minimum value of a column from the entire table.

SQL提供了一个非常易于使用的函数,可帮助您从整个表中查找列的最小值。


SELECT MIN(author_pay) FROM authors;

32.查找最大值 (32. Finding Maximum Values)

Similar to the function above, we also have the MAX function to find the maximum value from a specific column.

与上面的函数类似,我们还具有MAX函数,可从特定列中查找最大值。


SELECT MAX(author_pay) FROM authors;

33.创建别名 (33. Creating Aliases)

With the use of the AS keyword, you can change the name of the columns for display. Have a look at the example below to understand better.

通过使用AS关键字,您可以更改要显示的列的名称。 请看下面的示例以更好地理解。


SELECT author_name AS "Author Name", author_email
AS "Author Emails" FROM authors;

This will change the display column names to Author Name and Author Emails. You can use the AS keyword for columns that result from a function operation too.

这会将显示列名称更改为Author NameAuthor Emails 。 您也可以将AS关键字用于函数操作产生的列。


SELECT MIN(author_pay) AS "Lowest Salary" FROM authors;

34.内部联接两个表 (34. Inner Join Two Tables)

We’ve seen joins in better detail in a previous tutorial so we’ll briefly go over the joins here. Inner Joins will return all the matched values from both the tables. We’ll look at other joins briefly in the coming few points.

在上一教程中 ,我们已经对连接进行了更详细的介绍,因此我们将在此处简要介绍连接。 内部联接将从两个表中返回所有匹配的值。 在接下来的几节中,我们将简要介绍其他联接。

Let’s assume we also have another database that keeps a track of the articles that are written by our authors and the common column between them is the author’s email. We’ll call that table “author_submissions”

假设我们还有另一个数据库,该数据库跟踪我们作者撰写的文章,并且它们之间的共同栏是作者的电子邮件。 我们称该表为“ author_submissions”


SELECT authors.author_name, authors.author_email, author_submissions.article_title FROM authors
INNER JOIN author_submissions ON authors.author_email=author_submissions.author_email;

Seems complicated? It really isn’t. We’re simply combining the columns from both the tables that we want to display, then matching them based on the “email” in both the tables. Have a read through the joins article and you’ll get a good understanding of this concept.

看起来复杂吗? 真的不是。 我们只需要合并要显示的两个表中的列,然后根据两个表中的“电子邮件”进行匹配。 通读joins文章,您将对该概念有很好的理解。

35.左外联接 (35. Left Outer Join )

Compared to the Inner join above, Left Outer Joins will return all the values from the left table and only the matching values from the right table.

与上面的内部联接相比,左外部联接将返回左表中的所有值,并且仅返回右表中的匹配值。

For rows from the right table which don’t get matched, the left join will mark them as NULL.

对于右表中未匹配的行,左联接会将其标记为NULL。


SELECT authors.author_name, authors.author_email, author_submissions.article_title FROM authors
LEFT JOIN author_submissions ON authors.author_email=author_submissions.author_email

36.右外连接 (36. Right Outer Join)

The right outer join, in contrast to the left outer join, will return all values from the right table while only returning the matched values from the left table and displaying NULL for empty rows.

与左外部联接相反,右外部联接将返回右表中的所有值,而仅返回左表中的匹配值并为空行显示NULL。


SELECT authors.author_name, authors.author_email, author_submissions.article_title FROM authors
RIGHT JOIN author_submissions ON authors.author_email=author_submissions.author_email

The queries remain the same, but what changes is the keyword that’s being used.

查询保持不变,但是所使用的关键字发生了变化。

37.完全外部联接 (37. Full Outer Joins)

The Full outer join combines the functionality of the Right and the Left Join in one. It returns all the values from both the tables and maks NULL for rows that don’t have a match.

完全外部联接将右联接和左联接的功能合二为一。 对于不匹配的行,它从表和maks NULL中返回所有值。


SELECT authors.author_name, authors.author_email, author_submissions.article_title FROM authors
FULL OUTER JOIN author_submissions ON authors.author_email=author_submissions.author_email

38.自我加入 (38. Self Join)

The self-join is a very simple join but might be confusing at first. We join the columns of the same table for display based on specific conditions that we provide.

自联接是一个非常简单的联接,但起初可能会造成混淆。 我们根据所提供的特定条件将同一表的列连接起来以进行显示。


SELECT a1.author_name AS Author_A, a2.author_name
AS Author_B, a1.author_email FROM authors a1, authors a2;

This will create a table that looks similar to the one below:

这将创建一个看起来类似于以下表的表:

Author_A Author_B author_email
Joe Joe joe@journaldev.com
Jane Jane jane@journaldev.com
John John john@journaldev.com
作者_A 作者_B author_email
joe@journaldev.com
jane@journaldev.com
约翰 约翰 john@journaldev.com

But what’s the use of this join? In our case, nothing. But when you’re working with products and orders, it helps you visually represent all the orders and products in the format while matching customers.

但是此联接的用途是什么? 在我们的情况下,什么都没有。 但是,当您使用产品和订单时,它可以帮助您在匹配客户的同时以格式直观地表示所有订单和产品。

For example, if you have a table with CustomerID, ProductID, City, Price in the same table, you can self join the table to display the City data in one column and match the CustomerID in two columns. This will help you understand how many customers are from the same city in a tabular form.

例如,如果您在同一表中有一个包含CustomerID,ProductID,City和Price的表,则可以自行联接该表以在一个列中显示City数据,并在两列中匹配CustomerID。 这将帮助您以表格的形式了解来自同一城市的多少客户。

40. SQL查询中的个案语句 (40. Case Statements in SQL Queries)

When listing data from a table, you can add a custom column that displays information conditionally based on the data that’s being compared with.

列出表中的数据时,您可以添加一个自定义列,该列根据要比较的数据有条件地显示信息。

Complicated? Think of it this way. Suppose you have grades of 1000s of students to work with and you need to figure out how many of them passed and how a man of them failed. It would be really useful if SQL could, based on a condition, print a “Pass” or “Fail” right after the marks, wouldn’t it?

复杂? 这样想吧。 假设您有数千名学生的工作成绩,并且您需要弄清楚其中有多少人通过了考试,其中有一个人失败了。 如果SQL可以根据条件在标记后立即打印“通过”或“失败”,这将非常有用,不是吗?

That’s one of the uses of the Case statements.

这就是Case语句的用途之一。


SELECT author_name, author_pay,
CASE
WHEN author_pay < 30000 THEN "New author"
WHEN author_pay > 60000 THEN "Experienced Author"
ELSE "Budding author"
END AS "Author Experience"
FROM authors

For our table, the above query will give an output like the one below.

对于我们的表,上面的查询将给出类似下面的输出。

Author_name author_pay Author Experience
Joe 50000 Budding Author
Jane 70000 Experienced Author
John 20000 New Author
作者名 author_pay 作者经验
50000 萌芽作者
70000 经验丰富的作者
约翰 20000 新作者

41.处理表输出中的NULL值 (41. Handling NULL Values in Table Outputs)

When using the SELECT keyword to return table values, you might need to handle the NULL values that come up within the table. That can be handled with the IFNULL keyword.

使用SELECT关键字返回表值时,可能需要处理表中出现的NULL值。 可以使用IFNULL关键字处理。


SELECT author_name, IFNULL(author_pay, 10000)
AS "Pay" FROM authors;

The above query will return the values of the authors and the salaries that are being paid to them. If an author doesn’t have a salary listed, it will automatically display 10,000 for the NULL value.

上面的查询将返回作者的值和支付给他们的薪水。 如果作者没有列出工资,它将自动显示10,000作为NULL值。

42.测试NULL值 (42. Testing For NULL Values)

If you have a list of values that are being passed to the SQL database and you need to test them for NULL-ness, you can use the COALESCE function.

如果您有要传递到SQL数据库的值列表,并且需要测试它们的NULL值,则可以使用COALESCE函数。

Why would you need it? Here’s an example. Suppose you want to display a bunch of data. Many of the data rows could have NULL values. You can test for the values by passing a column list to the COALESCE function and get the first non-null value outputted.

你为什么需要它? 这是一个例子。 假设您要显示一堆数据。 许多数据行可能具有NULL值。 您可以通过将列列表传递给COALESCE函数来测试这些值,并获得输出的第一个非空值。


SELECT COALESCE(author_name, author_pay)
AS "Coalesce Example"
Coalesce Example
Joe
Jane
John
合并示例
约翰

The above table is a representation of what the SQL query output would be. Why? Because none of the “author_name” rows are NULL.

上表是SQL查询输出的表示。 为什么? 因为所有“ author_name”行都不为空。

If either of the author_name values were NULL, we’d have the author_pay take place of the NULL value.

如果两个author_name值均为NULL,我们将使用author_pay代替NULL值。

43.连接两个字符串 (43. Joining Two Strings)

You can concatenate two strings with the help of the CONCAT function.

您可以借助CONCAT函数连接两个字符串。


SEELCT CONCAT ('String 1', 'String2')

Output: String 1String 2

输出:字符串1字符串2

You can add functions or queries in place of those strings to concatenate and use the AS keyword to name the newly formed column.

您可以添加函数或查询来代替这些字符串进行连接,并使用AS关键字命名新形成的列。

44.替换字符 (44. Replace Characters )

Looking for a way to use Regex to replace characters with other characters? This is the section that answers your question.

寻找使用Regex替换其他字符的方法吗? 这是回答您问题的部分。


SELECT TRANSLATE(author_name, 'j', 'c') from authors

This query will replace all the occurrences of the letter “j” in the entire column with the letter “c”.

该查询将用字母“ c”替换整个列中所有出现的字母“ j”。

You can replace all the parameters with regex as per your requirements.

您可以根据需要用regex替换所有参数。

45.更改字符串大小写 (45. Change String Case)

To display the output of a table in upper or lowercase, you can use the UPPER() and LOWER() functions.

要以大写或小写形式显示表的输出,可以使用UPPER()和LOWER()函数。


SELECT UPPER(author_name) FROM authors;
SELECT LOWER(author_name) FROM authors;

This will display the author names in the upper and lower case respectively.

这将分别以大写和小写形式显示作者姓名。

46.类型转换输入数据 (46. Typecasting Input Data)

We obviously want the data that’s being stored in our databases to be clean and error-free. And we can do our part by at least making sure that the data is of the correct type.

显然,我们希望存储在数据库中的数据是干净无误的。 而且,我们至少可以确保数据类型正确,才能做到这一点。


SELECT CAST(<value> AS <datatype>);
SELECT CAST("hello" AS varchar(50));

47.检查值是否为数值 (47. Check if Values are Numeric)

If you do not want to typecast and only want to check if the value being passed is numeric or not, we can use the ISNUMERIC function.

如果您不想进行强制转换,而只想检查传递的值是否为数字,则可以使用ISNUMERIC函数。


SELECT ISNUMERIC(52);

Returns a 1 if the value is numeric, and a 0 if the value is not numeric.

如果值为数字,则返回1;如果值不是数字,则返回0。

48.返回ASCII值 (48. Return ASCII Values)

You can also use the ASCII() function to return ASCII values of any character that is passed to it.

您也可以使用ASCII()函数返回传递给它的任何字符的ASCII值。

If the string passed to the ASCII function as a parameter is longer than 1 character, the returned value is for the first character in the string.

如果作为参数传递给ASCII函数的字符串长于1个字符,则返回值是该字符串中的第一个字符。


SELECT ASCII("Hello");

The above will return “72” as the output because uppercase H has an ASCII value of 72.

上面将返回“ 72”作为输出,因为大写字母H的ASCII值为72。

49.在另一个字符串中查找一个字符串 (49. Find a String in Another String)

To check for occurrences of values between two columns, you can use the INSTR function in MySQL.

要检查两列之间是否存在值,可以在MySQL中使用INSTR函数。


SELECT INSTR(author_name, author_email)
AS MatchName from authors;

This will return the character position of the matched string. The “author_name” will be searched within “author_email”.

这将返回匹配字符串的字符位置。 将在“ author_email”中搜索“ author_name”。

50.修剪空白 (50. Trim Whitespace)

Ever stuck in a situation where you have a lot of values with whitespaces that you need to remove?

是否曾经遇到过需要删除大量带有空格的值的情况?

Fret not, because the TRIM() function solves this problem for us.

不用担心,因为TRIM()函数为我们解决了这个问题。


SELECT TRIM(author_name)
AS "Trimmed Names" FROM authors;

Other functions that can be used in a similar fashion are RTRIM and LTRIM to trim right and left trailing spaces respectively.

可以类似方式使用的其他功能是RTRIM和LTRIM,分别用于修剪左右尾随空格。

结论 (Conclusion)

We hope that you learned some new functions or queries from our tutorial above. If you have any questions, let us know in the comments below.

我们希望您从上面的教程中学到了一些新功能或查询。 如果您有任何疑问,请在下面的评论中告诉我们。

翻译自: https://www.journaldev.com/36886/top-best-sql-queries

sql查询前50条

sql查询前50条_您必须知道的前50条SQL查询相关推荐

  1. linux执行多个命令_您必须知道的前50多个Linux命令

    linux执行多个命令 Using Linux command on a regular basis? Today we'll look at 50+ Linux commands you must ...

  2. 查询分析器在哪里_你应该知道的3种Node.js分析器类型

    Node.js类似于许多其他编码语言,因为它需要与正确的工具结合使用来调试程序,克服任何瓶颈并优化其功能.使用正确的分析器,您可以毫不费力地实现这一目标,尽管它们都不是完美的. 在编码中,分析器是一种 ...

  3. PCB设计师需要知道的前5个PCB设计指南-PCB布线布局导致的焊接、装配问题

    PCB设计师需要知道的前5个PCB设计指南 在纸上或任何物理形式上设计真实的电路板的关键是什么? 让我们探讨设计一个可制造,功能可靠的PCB时需要了解的前5个设计指南. 工程师的5大PCB设计指南 在 ...

  4. pgsql 前10条_未来3年,广州83条城中村、285个旧街区将迎来改造

    文.图/羊城晚报全媒体记者 赵燕华 通讯员 穗建 未来3年,广州83条城中村将实施改造,285个旧街区将迎来改造,推进176个旧厂房改造,将整治1.24亿平方米违建-- 9月25日,在广州市深化城市更 ...

  5. sql还原数据库备份数据库_有关数据库备份,还原和恢复SQL面试问题–第一部分

    sql还原数据库备份数据库 So far, we've discussed a lot about database backup-and-restore process. The backup da ...

  6. sql还原数据库备份数据库_有关数据库备份,还原和恢复SQL面试问题–第二部分

    sql还原数据库备份数据库 In this article, we'll walk through, some of the refined list of SQL Server backup-and ...

  7. sql还原数据库备份数据库_有关数据库备份,还原和恢复SQL面试问题–第IV部分

    sql还原数据库备份数据库 In this article, we'll see the how the backup-and-restore meta-data tables store the i ...

  8. sql还原数据库备份数据库_有关数据库备份,还原和恢复SQL面试问题–第三部分

    sql还原数据库备份数据库 So far, we've discussed a lot about database backup commands. In this article, we'll d ...

  9. python特效进度条_六种酷炫Python运行进度条

    作者 | 行哥 来源 | 一行数据 之前行哥给大家推荐过一个windows神器,里面有个小功能是人生进度条,可以看到2020年的进度只剩下一半,那么你的代码进度还剩多少呢? 这不,行哥本文介绍了目前6 ...

最新文章

  1. 解决Git中fatal: refusing to merge unrelated histories
  2. 自由、开源及其敌人 —— RMS事件簿
  3. 本地存储之sessionStorage
  4. flask get 参数_用它 5 分钟以后,我放弃用了四年的 Flask
  5. 2016/1/14 java随机数生成
  6. XJOI 3266 Dyeing 染色 题解
  7. 洛谷P1589 泥泞路
  8. 编写安全的驱动程序之输入输出检查
  9. base64编码的图片字节流存入html页面中的显示
  10. jumpserver跳板机docker安装小小趟坑
  11. 如何删除PeopleSoft Process Definition
  12. 计算机软件服务可以自开专票,新规:小规模纳税人也可以自开专票
  13. c语言转换绝对值函数的程序,c语言abs(c语言求绝对值的程序)
  14. python逻辑回归模型建模步骤_逻辑回归建模及变量重要性可视化(Python实现)
  15. sublime使用技巧
  16. C语言卡路里程序,燃烧app的卡路里--app瘦身之路
  17. WINVNC源码阅读(二)
  18. java中peek是什么意思,在Java流中,PEEK真的只用于调试吗?
  19. 微服务-高并发-思路
  20. 记毕业季——回忆四年大学,青春无悔【正能量】

热门文章

  1. C++ Pitfalls 之 reference to an object in a dynamically allocated containter
  2. kettle的hello world
  3. java中的异常和处理
  4. 【Git入门之十四】Git GUI
  5. Python 基础课程第五天
  6. springboot-mybatis-多数据源
  7. 作业1-3 求1+2!+3!+...+20!的和
  8. 【BZOJ 1038】 1038: [ZJOI2008]瞭望塔
  9. NPOI 读取excel到DataTable 读取隐藏列 读取公式列
  10. 走吧---------------北岛