MySQL是一種關(guān)系型數(shù)據(jù)庫管理系統(tǒng)。查詢相同數(shù)據(jù)并修改相同數(shù)據(jù)是使用MySQL的一個常見操作。
在MySQL中,使用SELECT語句可以查詢表中的數(shù)據(jù)。例如,我們有一個名為“customers”的表,其中包含了客戶的相關(guān)信息,如下所示:
SELECT * FROM customers; +----+-----------+----------+---------------------+ | id | name | email | created_at | +----+-----------+----------+---------------------+ | 1 | John Doe | jdoe@exa | 2019-01-01 00:00:00 | | 2 | Jane Doe | jadoe@exa| 2019-01-02 00:00:00 | | 3 | Bob Smith | bsmith@ex| 2019-01-03 00:00:00 | | 4 | Alice Sim | asim@exa | 2019-01-04 00:00:00 | | 5 | Bill Lee | blew@exa | 2019-01-05 00:00:00 | +----+-----------+----------+---------------------+
若要查詢名字為“Jane Doe”的客戶,使用以下SELECT語句:
SELECT * FROM customers WHERE name='Jane Doe'; +----+----------+----------+---------------------+ | id | name | email | created_at | +----+----------+----------+---------------------+ | 2 | Jane Doe | jadoe@exa| 2019-01-02 00:00:00 | +----+----------+----------+---------------------+
若要修改姓名為“Jane Doe”的客戶名字為“Jane Smith”,使用以下UPDATE語句:
UPDATE customers SET name='Jane Smith' WHERE name='Jane Doe'; SELECT * FROM customers; +----+------------+----------+---------------------+ | id | name | email | created_at | +----+------------+----------+---------------------+ | 1 | John Doe | jdoe@exa | 2019-01-01 00:00:00 | | 2 | Jane Smith | jadoe@exa| 2019-01-02 00:00:00 | | 3 | Bob Smith | bsmith@ex| 2019-01-03 00:00:00 | | 4 | Alice Sim | asim@exa | 2019-01-04 00:00:00 | | 5 | Bill Lee | blew@exa | 2019-01-05 00:00:00 | +----+------------+----------+---------------------+
在使用UPDATE語句時,需注意WHERE子句中的條件,確保只修改想要修改的數(shù)據(jù)。