【MySQL】 Data Search: Mastering SELECT Statements, WHERE Clauses, and Operators

本記事にはプロモーションが含まれています。

軽くて安いのに大容量!カフェ作業も捗る!プログラミングに最適なPCをお探しなら【おすすめはこちら

Data search in MySQL is a fundamental aspect of database management and is essential for efficient operation.

In this article, we begin with the basics of the SELECT statement and then delve into the use of crucial operators like AND, OR, and NOT within the WHERE clause.

By mastering these operators, you will learn how to efficiently extract necessary information from complex data sets.

What You Will Learn in This Article
  • Basics of the SELECT Statement
  • The Role of the WHERE Clause
  • Using the AND Operator
  • Utilizing the OR Operator
  • Applying the NOT Operator

▼youtube

youtube
【知識0からMySQL】データの検索①データベース作成

youtube
【MySQL】データの検索|WHERE句 LIKE 集計関数 AS GROUP BY ORDER BY LIMIT サブクエリ

The Database and Tables We’ll Use in This Session

In this exploration of data search, we will create a database for managing school-related data and explain the process of constructing the necessary tables within it.

We will create two tables: the ‘students’ table, which contains information about the students, and the ‘departments’ table, detailing the departments to which students belong.

  • ‘Students’ Table: This table stores various details about the students, including their names, grades, and department IDs.
  • ‘Departments’ Table: This table records information related to each department, such as the department name and its quota.”

students Table

idID Number
nameStudent Name
gradeGrade
department_idID of the Department the Student Belongs To
entry_examEntrance Exam Score
statusEnrollment Status
updated_atDate Updated

departments

departments Table

idID Number
nameDepartment Name
quotaCapacity

SQL Script: Creating a Database and Adding Data

Data Search with the SELECT Statement

SELECT column_name
FROM table_name
WHERE condition;

  • List the column names separated by commas.
  • Use ‘*’ for column_name to select all columns.
  • Specify the search target using the WHERE clause.”

Retrieve all data from the students table:

Retrieve the student name and grade for the student with id 1:

Operators used in the WHERE clause:

=Equals
<Less than
<=Less than or equal to
>Greater than
>=Greater than or equal to
<>Not equal to
!=
  • Retrieve member data for the member named ‘Yamanaka Shinji’:
  • Search for members in the 3rd grade or higher:
  • Retrieve student information updated on or after May 5th, 2005:

Operators for Connecting Conditional Expressions:

ANDAnd
OROr
NOTNot
  • Search for students in the 3rd grade or higher who have had their information updated on or after May 5th, 2005:
  • Search for members who are in the 3rd grade or higher, or whose entrance exam score is not below 80:

Summary: Essentials of Data Search in MySQL Using SELECT and WHERE Operators

In this article, we have thoroughly explained the basics of data search in MySQL using the SELECT statement and the operators (AND, OR, NOT) used in the WHERE clause. The SELECT statement is a fundamental tool for selecting necessary information from a database. On the other hand, the WHERE clause is used to filter data based on specific conditions.

The operators AND, OR, and NOT are essential for making these queries more flexible and for meeting complex data requirements. The AND operator is used to select data when all conditions are true. The OR operator is used when any of the conditions is true. And the NOT operator is used to negate a specific condition.

Such combinations of operators enable more precise data searches.

タイトルとURLをコピーしました