迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 数据库 > MySQL >

MySQL ForEach 循环

作者:迹忆客 最近更新:2023/05/06 浏览次数:

本篇文章介绍如何在一条语句中使用 INSERT、SELECT、WHERE 和 JOIN 模拟 MySQL 中的 foreach 循环。


MySQL foreach 循环

为了理解 foreach 循环模拟,让我们创建三个表,其名称和属性名称如下所示。

表格和各自的属性:

users -> id, user_name, person_id, email
person -> id,  person_name, address_id
address -> id, email

我们使用以下语句来创建表。

# create `address`, `person`, and `users` tables
CREATE TABLE address(
  id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  email VARCHAR(45) NULL
);

CREATE TABLE person (
  id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  person_name VARCHAR(45) NOT NULL,
  address_id INT NULL,
  FOREIGN KEY (address_id) REFERENCES address(id)
  );

CREATE TABLE users (
  id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  user_name VARCHAR(45) NOT NULL,
  person_id INT NOT NULL,
  email VARCHAR(45) NOT NULL,
  FOREIGN KEY (person_id) REFERENCES person(id)
  );

此外,使用下面的 MySQL 查询将数据插入到我们刚刚创建的表中。

# insert data into the `address` table. Here
# we have parenthesis only because one field
# is `auto_incremented`, and the other can accept
# `null`.
INSERT INTO address () VALUES (),(),(),();

# insert data into the `person` table
INSERT INTO person (person_name)
VALUES
('Thomas Christopher'),
('Jim James'),
('Mehvish Ashiq'),
('Saira Daniel');

# insert data into the `users` table
INSERT INTO users(user_name, person_id, email)
VALUES
('chthomas', 1, 'chthomas@yahoo.com'),
('jjames', 2, 'jimjames@gmail.com'),
('mehvishashiq', 3, 'mehvish@yahoo.com'),
('danielsaira', 4, 'sairad@gmail.com');

接下来,我们使用 SELECT 语句查看最近的数据。

# display data for all the specified tables
SELECT * FROM users;
SELECT * FROM person;
SELECT * FROM address;

输出(对于 users 表):

+----+--------------+-----------+--------------------+
| id | user_name    | person_id | email              |
+----+--------------+-----------+--------------------+
|  1 | chthomas     |         1 | chthomas@yahoo.com |
|  2 | jjames       |         2 | jimjames@gmail.com |
|  3 | mehvishashiq |         3 | mehvish@yahoo.com  |
|  4 | danielsaira  |         4 | sairad@gmail.com   |
+----+--------------+-----------+--------------------+
4 rows in set (0.00 sec)

输出(对于 person 表):

+----+--------------------+------------+
| id | person_name        | address_id |
+----+--------------------+------------+
|  1 | Thomas Christopher |       NULL |
|  2 | Jim James          |       NULL |
|  3 | Mehvish Ashiq      |       NULL |
|  4 | Saira Daniel       |       NULL |
+----+--------------------+------------+
4 rows in set (0.00 sec)

输出(对于 address 表):

+----+-------+
| id | email |
+----+-------+
|  1 | NULL  |
|  2 | NULL  |
|  3 | NULL  |
|  4 | NULL  |
+----+-------+
4 rows in set (0.11 sec)

如果 users.person_id 不为空且 person.address_id 为空,我们应该在地址表中创建一条新记录。 新创建的行将具有与 users.email 完全相同的电子邮件。

在这里,我们可以通过以下方式模拟 foreach 循环。

# simulation of `foreach loop`
INSERT INTO address (email) SELECT users.email
FROM users JOIN person ON users.person_id = person.id
WHERE person.address_id IS NULL;

接下来,使用 SELECT 查询查看更新后的数据。

SELECT * FROM address;

输出:

+----+--------------------+
| id | email              |
+----+--------------------+
|  1 | NULL               |
|  2 | NULL               |
|  3 | NULL               |
|  4 | NULL               |
|  5 | chthomas@yahoo.com |
|  6 | jimjames@gmail.com |
|  7 | mehvish@yahoo.com  |
|  8 | sairad@gmail.com   |
+----+--------------------+
8 rows in set (0.00 sec)

但是,我们也可以使用游标和过程来执行上述查询。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

如何在 MySQL 中声明和使用变量

发布时间:2024/03/26 浏览次数:115 分类:MySQL

当你需要在 MySQL 中的脚本中存储单个值时,最好的方法是使用变量。变量有不同的种类,有必要知道何时以及如何使用每种类型。

在 MySQL 中实现刷新权限

发布时间:2024/03/26 浏览次数:211 分类:MySQL

本教程介绍了 MySQL 中的刷新权限命令,用于刷新授权表并影响允许的更改。

在 MySQL 中设置时区

发布时间:2024/03/26 浏览次数:93 分类:MySQL

在本教程中,我们将学习如何在 MySQL 服务器中更改时区。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便