0

正如标题所暗示的那样,我试图在两个不同的表中使用单个查询来更新多行,其中 id 等于从第三个表中选择的结果。以下是示例表格布局。

CREATE TABLE hampster_families (
    id INT(13) NOT NULL AUTO_INCREMENT,
    family_name varchar(255) NOT NULL UNIQUE,
    fuzzy_name varchar(255),
    tags varchar(255),
    date_added TIMESTAMP NOT NULL DEFAULT NOW(),
        PRIMARY KEY (id)

CREATE TABLE apple_addresses (
    id INT(13) NOT NULL AUTO_INCREMENT,
    address VARCHAR(39) NOT NULL UNIQUE,
    monitor BOOLEAN NOT NULL DEFAULT 0,
    date_added TIMESTAMP NOT NULL DEFAULT NOW(),
    hampster_family int(7) NOT NULL DEFAULT 0,
        PRIMARY KEY (id)

CREATE TABLE orange_addresses (
    id INT(13) NOT NULL AUTO_INCREMENT,
    address varchar(200) NOT NULL UNIQUE,
    monitor BOOLEAN NOT NULL DEFAULT 0,
    date_added TIMESTAMP NOT NULL DEFAULT NOW(),
    hampster_family INT(7) NOT NULl DEFAULT 0,
        PRIMARY KEY (id)

我正在尝试将 orange_addresses.monitor、apple_addresses.monitor 更新为等于 1,其中 orange_addresses.hampster_family、apple_addresses.hampster_family 等于从 SELECT id FROM hampster_families WHERE hampster_fammilies.family_name = "African Rabid" 返回的 id

我已经研究过使用存储变量,但是(我在这里缺乏 SQL 能力)因为这个查询需要一个变量,我不认为存储变量会起作用。鉴于此,我已经研究过使用连接来完成此操作,但我从根本上误解了 UPDATE/JOIN 组合的语法。因此,我在这里寻找一些启示!:)

4

2 回答 2

2

一步一步:这是您的基本查询:

 SELECT id 
 FROM hampster_families 
 WHERE hampster_fammilies.family_name = "African Rabid"

现在,你想要所有 orange_addresses,其中 orange_addresses.hampster_family 是那个 id,所以,加入:

 SELECT hampster_families.id 
 FROM hampster_families
 LEFT JOIN orange_adresses
     ON orange_addresses.hampster_family = hampster_families.id
 WHERE hampster_fammilies.family_name = "African Rabid"

apple_addresses 也是如此:

 SELECT hampster_families.id 
 FROM hampster_families
 LEFT JOIN orange_adresses
     ON orange_addresses.hampster_family = hampster_families.id
 LEFT JOIN apple_adresses
     ON apple_addresses.hampster_family = hampster_families.id
 WHERE hampster_fammilies.family_name = "African Rabid"

我们对他们的监控栏感兴趣

 SELECT orange_addresses.monitor, apple_addresses.monitor
 FROM hampster_families
 LEFT JOIN orange_adresses
     ON orange_addresses.hampster_family = hampster_families.id
 LEFT JOIN apple_adresses
     ON apple_addresses.hampster_family = hampster_families.id
 WHERE hampster_fammilies.family_name = "African Rabid"

使其成为更新:在集合之后移动选定的字段,使 SELECT FROM an UPDATE:

 UPDATE hampster_families
 LEFT JOIN orange_adresses
     ON orange_addresses.hampster_family = hampster_families.id
 LEFT JOIN apple_adresses
     ON apple_addresses.hampster_family = hampster_families.id
 SET 
   orange_addresses.monitor = 1
   apple_addresses.monitor  = 1
 WHERE hampster_fammilies.family_name = "African Rabid"
于 2013-06-05T16:49:49.770 回答
1

这对我有用。刚检查。

UPDATE orange_addresses, apple_addresses
SET orange_addresses.monitor=1,apple_addresses.monitor=1
WHERE orange_addresses.hampster_family = apple_addresses.hampster_family
    AND orange_addresses.id = (
        SELECT id 
        FROM hampster_families 
        WHERE hampster_families.family_name = 'Test' 
        LIMIT 1);
于 2013-06-05T16:25:23.847 回答